转换的UIImageView和直接添加的UIImageView不同在于:你不能直接在面板中设置UIImageView的image需要在viewDidload中 进行设置
swift用xib自定义View的方法如下:
1. 创建一个IDView,添加一个IDView.Xib
2. 对IDCard.xib添加约束
3、在IDCard.xib的 File's Owner class 设置为IDCard:
4、在IDCard.swift中添加如下代码,把xib的view连线到代码上的contentView:
5、绑定xib,实现 @IBInspectable, @IBDesignable这几部分代码
@IBDesignable
class IDCard: UIView {
@IBOutlet var contentView: UIView!
@IBInspectable
var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius >0
}
}
@IBInspectable
var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable
var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.CGColor
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initialFromXib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialFromXib()
}
func initialFromXib() {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "IDCard", bundle: bundle)
contentView = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
contentView.frame = bounds
addSubview(contentView)
}
}
6、运行代码,结果如下
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)