I am trying to make a custom UIButton subclass that has different colors in the normal
, selected
, and disabled
states. My button lives in a framework that is then imported into an app, but every code snippet I place here, I have tried in both the main app and the framework--I know it shouldn't make any difference, but I wanted to cover my bases. I can't get it to work to save my life.
class BrokenButton: UIButton {
override var isEnabled: Bool {
didSet {
print("This is never called no matter what I do")
}
}
}
I've tried using KVO to watch the value of isEnabled
since overriding the setter did not work:
class BrokenButton2: UIButton {
required init() {
super.init(frame: .zero)
addObserver(self, forKeyPath: #keyPath(isEnabled), options: [.new], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
print("Never called")
}
}
I'm at my wit's end here. What am I getting wrong about this?