首页 > 解决方案 > UIButton 属性观察者 didset 从未调用过

问题描述

我在 cocoapods 库中有一个简单的按钮,当它被调用时我需要更改 tintcolorisHighlighted但是 isHighlighted 被调用但 didset 或 willset 永远不会被调用。

我当然将我的按钮设置为突出显示,并且在我将 tableview 单元格出列后调用此函数

func setupCell() {
    let myCustomButton = UIButton.button(title: "Button")
    myCustomButton.isHighlighted = true
}

我的 UIButton 扩展

extension UIButton {

    public static func button(title: String) -> UIButton {
        let button = UIButton(type: .custom)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        return button
    }

  override open var isHighlighted: Bool {
      didSet {
          print("Never gets here, ever!")
      } 
   }
}   

如何触发 didSet?除了将 isHighlighted 设置为 true 之外,还有其他方法吗?

标签: iosswiftuibutton

解决方案


推荐阅读