Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customizing UISearchBarIconClear for UIControlStateHighlighted does not work

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!

like image 274
Dima Avatar asked Feb 06 '13 01:02

Dima


2 Answers

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];
like image 130
chu_tracy Avatar answered Sep 30 '22 02:09

chu_tracy


How about using appearance proxy?

[[UIButton appearanceWhenContainedIn:[UITextField class], [UISearchBar class], nil] setImage:myImage forState:UIControlStateHighlighted];
like image 37
jayjunck Avatar answered Sep 30 '22 02:09

jayjunck