[英]Why does this IBDesignable class not display its changes on storyboard?
I have an IBDesignable subclass of UIButton, but my changes don't appear on the storyboard where I use it. 我有UIButton的IBDesignable子类,但是我的更改未出现在使用它的情节提要中。 I have tried refreshing all views on my storyboard, or re-linking the button with the class, but to no avail.
我尝试刷新情节提要上的所有视图,或将按钮与课程重新链接,但无济于事。
The class: 班级:
@IBDesignable class MyButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
layer.cornerRadius = 3
layer.masksToBounds = true
backgroundColor = Colors.E9511C
setTitleColor(.white, for: .normal)
titleLabel?.font = Fonts.openSansBold(14)
}
}
@IBDesignable class MyButton: UIButton { @IBDesignable类MyButton:UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
@IBInspectable var borderColor: UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable var cornerRadius: Int = 1 {
didSet {
layer.cornerRadius = CGFloat(cornerRadius)
}
}
func commonInit() {
layer.cornerRadius = CGFloat(cornerRadius)
layer.masksToBounds = true
layer.borderColor = borderColor.cgColor
//layer.bobackgroundColor = backgroundColor.cgColor // Colors.E9511C
setTitleColor(.white, for: .normal)
titleLabel?.font = UIFont.systemFont(ofSize: 14) // Fonts.openSansBold(14)
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
commonInit()
}
} }
Add this to your MyButton
class: 将此添加到您的
MyButton
类:
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
commonInit()
}
You may also be running into an issue with your Colors
and / or Fonts
... this full class works fine for me: 您可能还遇到了
Colors
和/或Fonts
...这个完整的课程对我来说很好:
@IBDesignable class MyButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
layer.cornerRadius = 3
layer.masksToBounds = true
backgroundColor = UIColor.brown // Colors.E9511C
setTitleColor(.white, for: .normal)
titleLabel?.font = UIFont.systemFont(ofSize: 14) // Fonts.openSansBold(14)
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
commonInit()
}
}
Result in Storyboard / Interface Builder: 故事板/界面生成器中的结果:
转到情节提要->身份检查器(在那里您必须将自定义类名设置为MyButton)清理项目,然后构建希望它能正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.