【问题标题】:Why does this IBDesignable class not display its changes on storyboard?为什么这个 IBDesignable 类不在情节提要上显示其更改?
【发布时间】:2018-07-10 11:46:47
【问题描述】:

我有一个 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)
    }
}

【问题讨论】:

    标签: ios uistoryboard ibdesignable


    【解决方案1】:

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

    }

    【讨论】:

      【解决方案2】:

      将此添加到您的 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()
          }
      }
      

      Storyboard/Interface Builder 中的结果:

      【讨论】:

        【解决方案3】:

        转到故事板 -> 身份检查器(您必须将自定义类名设置为 MyButton) 清理项目然后构建 希望它会工作

        【讨论】:

        • 它在我构建时工作,只是没有出现在故事​​板上
        • 使用 @IBInspectable ,然后检查 mainstoryboard(属性检查器)中的更改。检查上面的代码。它正在工作。
        猜你喜欢
        • 2017-12-08
        • 2019-09-29
        • 2015-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-01
        • 2016-01-07
        • 1970-01-01
        相关资源
        最近更新 更多