I have an IBDesignable subclass of UIButton, but my changes don't appear on the storyboard where I use it. 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 {
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:
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:
@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)清理项目,然后构建希望它能正常工作
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.