0

I have a simple button in a cocoapods library, I need to change the tintcolor when it is isHighlighted however isHighlighted gets called but didset or willset never get called.

I am ofcourse setting my button to be highlighted, and I call this function after I dequeue my tableview cell

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

My UIButton Extension

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!")
      } 
   }
}   

How can I trigger didSet? Is there any other way apart from setting isHighlighted to true?

久美子
  • 101
  • 1
  • 13
  • I tried the code it's working where do you write button's initialization code ?? maybe the code just not called . – M Ohamed Zead Oct 22 '20 at 12:42
  • Are you calling ```myCustomButton.isHighlighted = true``` inside an ```init```? – finebel Oct 22 '20 at 13:14
  • In case you do, check this: https://stackoverflow.com/questions/25230780/is-it-possible-to-allow-didset-to-be-called-during-initialization-in-swift – finebel Oct 22 '20 at 13:23
  • @finebel I don't have it in an init, I actually have it in a function which I call after dequeue my tableview cell – 久美子 Oct 22 '20 at 16:37

0 Answers0