swift使用xib绘制UIView
来源:互联网 发布:2002文泰刻绘软件下载 编辑:程序博客网 时间:2024/06/05 23:58
目标:用xib绘制一个UIView,在某个ViewController中调用。
三个文件:ViewController.swift DemoView.swift DemoView.xib
首先,可以专心将DemoView.xib画出来,别忘记DemoView.xib中UIView的一处设置
然后,写DemoView.swift文件,代码如下:
- class CoreView: UIView {
- //MARK:
- //MARK: properties
- @IBOutlet weak var makefriendsBtn: UIButton!
- @IBOutlet weak var networkBtn: UIButton!
- @IBOutlet weak var everyoneBtn: UIButton!
- //MARK:
- //MARK: constraints
- @IBOutlet weak var makefriendsBtnWidth: NSLayoutConstraint!
- @IBOutlet weak var networkBtnWidth: NSLayoutConstraint!
- @IBOutlet weak var everyoneBtnWidth: NSLayoutConstraint!
- //MARK:
- //MARK: functions
- required init(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- override func drawRect(rect: CGRect) {
- makeupUI()
- }
- func makeupUI() {
- self.layer.masksToBounds = true
- self.layer.cornerRadius = 3
- makefriendsBtn.layer.borderWidth = 1
- makefriendsBtn.layer.cornerRadius = 3
- makefriendsBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
- makefriendsBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
- makefriendsBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
- networkBtn.layer.borderWidth = 1
- networkBtn.layer.cornerRadius = 3
- networkBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
- networkBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
- networkBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
- everyoneBtn.layer.borderWidth = 1
- everyoneBtn.layer.cornerRadius = 0
- everyoneBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
- everyoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
- everyoneBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
- makefriendsBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 10
- networkBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 2
- everyoneBtnWidth.constant = (self.frame.width - 32 - 29) / 3 - 2
- }
- func buttonSelected(button: UIButton) {
- button.selected = !button.selected
- if button.selected == true {
- button.backgroundColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1)
- } else {
- button.backgroundColor = UIColor.whiteColor()
- }
- }
- }
下面就可以在ViewController.swift中调用了:
- var myView = NSBundle.mainBundle().loadNibNamed("DemoView", owner: nil, options: nil).first as? DemoView
- myView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width-50, height: self.view.frame.height-140)
- myView?.center = self.view.center
- if myView != nil {
- self.view.addSubview(myView!)
- }
0 0
- swift使用xib绘制UIView
- iOS swift使用xib绘制UIView
- iOS swift使用xib绘制UIView
- 使用XIB自定义UIView
- 使用xib布局UIView
- 使用XIB自定义UIView
- Swift中使用Xib获取UIView控件,结果UIView控件的subViews为nil
- swift中使用xib
- swift中的xib使用
- iOS使用xib自定义uiview
- iOS使用xib自定义uiview
- iOS使用xib自定义UIView
- 使用XIB自定义UIView子类
- [iOS 使用xib自定义UIView]
- swift -UIView的使用
- IOS开发 使用XIB自定义一个UIView
- IOS开发 使用XIB自定义一个UIView
- IOS开发 使用XIB自定义一个UIView
- ionic AngularJS-设定ion-scroll div 动态高度
- Git file
- C#下使用protobuf(Google Protocol Buffers)
- java不定长参数的简单使用
- linux磁盘格式化
- swift使用xib绘制UIView
- Python3 迭代器与生成器
- AudioContext接口的API整理
- Http请求
- C++ function bind及策略模式
- VIM常用命令
- CodeForces 660D Number of Parallelograms(水题)
- Apache搭建多个站点方法详解
- Cocos2dx 3.x的android studio项目之间.so库、资源移植