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

时间:2018-07-10 11:46:48

标签: ios uistoryboard ibdesignable

我有UIButton的IBDesignable子类,但是我所做的更改未显示在使用它的情节提要中。我尝试刷新情节提要上的所有视图,或将按钮重新与班级联系,但无济于事。

课程:

@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)
    }
}

3 个答案:

答案 0 :(得分:0)

@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()
}

}

答案 1 :(得分:-1)

将此添加到您的MyButton类中:

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    commonInit()
}

可能也遇到了您的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()
    }
}

故事板/界面生成器中的结果:

enter image description here

答案 2 :(得分:-2)

转到情节提要->身份检查器(在那里您必须将自定义类名称设置为MyButton) 清理项目然后构建 希望它能工作