简体   繁体   English

为什么此IBDesignable类不能在情节提要板上显示其更改?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM