ios - 为什么这个 IBDesignable 类不在 Storyboard 上显示其更改?

标签 ios uistoryboard ibdesignable

我有一个 UIButton 的 IBDesignable 子类,但我的更改没有出现在我使用它的 Storyboard 上。我尝试刷新 Storyboard上的所有 View ,或将按钮与类重新链接,但无济于事。

类:

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

}

关于ios - 为什么这个 IBDesignable 类不在 Storyboard 上显示其更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51264370/

相关文章:

ios - 在 CocoaPods 1.5 中使用静态库在导入时没有这样的模块

android - 我的 phonegap 应用程序退出后,iOS 会清除缓存吗?

objective-c - 防止在 segue 中关闭 View Controller

ios - Xcode 6 @IBInspectable : Initialize to starting value?

ios - UITextField-在文本溢出时删除椭圆

ios - 使用 NSURLSession 进行 HTTP 基本身份验证

ios - 我可以在 iOS 8 中使用重构的 Storyboard吗?

objective-c - 原型(prototype) UITableViewCell 不在 Storyboard 中的 UITableView 顶部

ios - 渐变不填充 IBDesignable UIView

iOS。 Interface Builder @IBInspectable 崩溃