At some point in my app I have a highlighted UIButton
(for example when a user has his finger on the button) and I need to change the background color while the button is highlighted (so while the finger of the user is still on the button).
I tried the following:
_button.backgroundColor = [UIColor redColor];
But it is not working. The color remains the same. I tried the same piece of code when the button is not highlighted and it works fine. I also tried calling -setNeedsDisplay
after changing the color, it didn't have any effect.
How to force the button to change the background color?
if you won't override just set two action touchDown touchUpInside
Try this !!!!
For TouchedDown Event set One color and for TouchUpInside set the other.
You can use this category which add the method setBackgroundColor:forState:
https://github.com/damienromito/UIButton-setBackgroundColor-forState-
My best solution for Swift 3+ without subclassing.
With this extension it's easy to manage colors for different states and it will fade your normal color automatically in case highlighted color is not provided.
Try
tintColor
:Drop it in and you're good to go:
*proerty can be set in IB, and if no highlighted background is set, background will not change when pressed