I can't believe I cannot figure this out. After migrating to Swift 3 yesterday I got everything but one thing working.
I created a custom button subclassing from UIButton. Upon selection I want to change the background color. This worked in Swift 2.2. Now I cant change the backgroundcolor when the button's been pressed. I can still change the background color of a UIButton, but not on the subclassed button.
What changed in Swift 3, am I missing something? The color for setTitleColor does change by the way.
Code:
func selectButton(_ button: CustomButton) {
button.isSelected = true
button.backgroundColor = UIColor.red
button.setTitleColor(UIColor.white, for: .selected)
}
When I add printstatements, I see that the output is different, but the view (color) hasn't changed.
print("CHANGE COLOR FROM: \(button.backgroundColor)")
button.backgroundColor = UIColor.red
button.setTitleColor(UIColor.white, for: .selected)
button.setNeedsDisplay()
print("CHANGE COLOR TO: \(button.backgroundColor)")
Output:
CHANGE COLOR FROM: Optional(UIExtendedGrayColorSpace 1 1)
CHANGE COLOR TO: Optional(UIExtendedSRGBColorSpace 1 0 0 1)