I\'ve a UIButton and I\'ve created an extension to add background color for different state.
I\'m using the following code:
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 ])