UIButton background color for highlighted/selected state issue

前端 未结 6 1859
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 18:47

I\'ve a UIButton and I\'ve created an extension to add background color for different state.

I\'m using the following code:



        
6条回答
  •  花落未央
    2021-02-18 19:31

    The reason it does not work is that you have not set the correct state: you are missing the [ .highlighted, .selected ] state, and so it falls back to plain .normal.

    When both isSelected and isHighlighted are true, UIKit looks explicitly for the state-combination of the two, and will not accept any of them individually.

    The original code extended with the missing state (and updated to valid syntax in Swift 5):

    self.donateButton.setBackgroundColor(.green, for: [ .highlighted, .selected ])
    self.donateButton.setTitleColor(.white, for: [ .highlighted, .selected ])
    

提交回复
热议问题