swift使用xib绘制UIView
2016-04-15 14:42
387 查看
目标:用xib绘制一个UIView,在某个ViewController中调用。
三个文件:ViewController.swift DemoView.swift DemoView.xib
首先,可以专心将DemoView.xib画出来,别忘记DemoView.xib中UIView的一处设置
然后,写DemoView.swift文件,代码如下:
[objc] view
plain copy
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中调用了:
[objc] view
plain copy
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!)
}
三个文件:ViewController.swift DemoView.swift DemoView.xib
首先,可以专心将DemoView.xib画出来,别忘记DemoView.xib中UIView的一处设置
然后,写DemoView.swift文件,代码如下:
[objc] view
plain copy
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中调用了:
[objc] view
plain copy
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!)
}
相关文章推荐
- Swift - 按钮(UIButton)的用法
- Swift写的超级简单的五子棋
- Swift写的超级简单的五子棋
- Swift:playground
- Swift 取子字符串
- Swift中文教程(一)基础数据类型
- Swift笔记:函数
- Swift笔记:For-In、While 循环、if条件语句
- Swift在导航栏左侧或者右侧放置多个按钮
- Swift UIPickerView
- Swift概览
- Swift 2.0初探
- 【Swift学习】Swift编程之旅---字符与字符串(五)
- Swift/OC计时器使用方法
- Swift中构造方法的解析
- Swift UI
- Swift-画虚线、虚线圆
- Swift、OC调用Framework
- swift一些常用的第三方的库
- swift 枚举详解