I have a UISearchBar for which I have set a custom UISearchBarIconClear for UiControlStateNormal.
[mySearchBar setImage:myImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
This part works as it should but unfortunately when tapping the clear button, it changes from the image I set, to the original default gray one.
I have tried setting the image for UIControlStateHighlighted, but apparently that does not work.
The documentation actually states
Valid states are UIControlStateNormal and UIControlStateDisabled.
What's the point of setting a custom button for the default state if you can't set it for the highlighted state? Am I missing something? Any thoughts or workarounds appreciated, thanks!
I had the same problem, and adjusting the order of the statements solved it:
So instead of doing UIControlStateNormal
first then UIControlStateHighlighted
, set the image for Highlighted state first
[searchBar setImage:clearIcon forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
[searchBar setImage:clearIcon forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
How about using appearance proxy?
[[UIButton appearanceWhenContainedIn:[UITextField class], [UISearchBar class], nil] setImage:myImage forState:UIControlStateHighlighted];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With