【问题标题】:IBDesignable class don't seems to work properlyIBDesignable 类似乎无法正常工作
【发布时间】:2019-06-13 22:38:20
【问题描述】:

我正在使用 swift 构建一个新的 iOS 应用程序,并尝试制作一个自定义 IBDesignable UIButton 类以在设计器中使用。 我的代码在运行时似乎在构建器或应用程序本身中都不起作用。 我给 IBInspectable 变量的默认值不适用于它们。

@IBDesignable class MyButton: UIButton {

@IBInspectable var backColor: UIColor = UIColor.red {
    didSet {
        self.backgroundColor = backColor
    }
}

@IBInspectable var textColor: UIColor = UIColor.white {
    didSet {
        self.setTitleColor(textColor, for: .normal)
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}

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

private func setup() {
    titleLabel?.font = UIFont.systemFont(ofSize: 17)
    contentEdgeInsets = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
    layer.cornerRadius = (frame.height / 2)
    setTitle(title(for: .normal)?.uppercased(), for: .normal)
}
}

我希望在设计器/应用程序中看到红色背景和白色文本的按钮,但这些都没有发生。 我正在使用 Swift 4.2 和 Xcode 10.1。

【问题讨论】:

    标签: ios swift ibdesignable


    【解决方案1】:

    添加这两个函数

      override func awakeFromNib() {
            super.awakeFromNib()
            self.setup()
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            self.setup()
        }
    

    不需要初始化

    【讨论】:

    • 谢谢,但没有帮助。
    【解决方案2】:

    设置属性默认值不会调用didSet 子句。
    你需要类似的东西:

        private func setup() {
            titleLabel?.font = UIFont.systemFont(ofSize: 17)
            contentEdgeInsets = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
            layer.cornerRadius = (frame.height / 2)
            setTitle(title(for: .normal)?.uppercased(), for: .normal)
            // I would do it differently, but this is just for demo purposes
            // Also, can't assign a prop to itself, so using a temp
            var tmp = self.backColor
            self.backColor = tmp
            tmp = self.textColor
            self.textColor = tmp
        }
    
    

    此外,Xcode 在 Swift 或 Objective C 中对 IBDesignable 不是很好。

    确保选中菜单项:Editor -> Automatically Refresh Views
    您也可以明确地执行 Editor -> Refresh All Views
    根据我的经验 - 这会在一段时间后停止工作,您需要退出 Xcode 并重新启动它以使其再次工作。

    【讨论】:

    • 它什么也没做......你有什么好的和完整的例子我可以使用吗?
    • @EitanaAviv 正如我所说,Xcode 对 IBDesignable 不是很好。尝试确保选中指定的菜单项,然后退出 Xcode 并重新启动。我用一个示例项目进行了尝试,它可以正常工作(重新启动 Xcode 后)。
    • @MosheGottlieb 感谢您拯救我的一天!
    【解决方案3】:
    1. 确保在 Interface Builder 中将按钮设置为键入 .custom。 这并不明显,但默认按钮类型为.system,无法自定义。我看到人们对此没有任何问题,但在我的应用程序中这一直是个问题。

    2. 另一个问题是,如果您将可检查的值保留为空,则不会调用 didSet 并且不会应用默认值。

      @IBInspectable var backColor: UIColor? = UIColor.red {
         didSet {
            updateBackground()
        }
      }
      
      private func updateBackground() {
         self.backgroundColor = backColor
      }
      
      func setup() {
         ...
         updateBackground()
      }
      

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2012-05-06
      • 2013-02-17
      • 2017-10-09
      • 2017-08-03
      • 2014-05-02
      • 2011-09-11
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多