2

I created a UISearchBar control with the bookmark icon displayed in the search field itself. I am now trying to look for ways for me to skin the bookmark button with an image. I have stumbled upon this post (refer to fourth comment) which seems to be able to do what I want to do, but only works for the x button in the search field and not the bookmark button.

I tried the following line to change the button, but it doesn't seem to work:

[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundImage:[UIImage imageNamed:@"BookmarkButtonNormal.png"] forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundImage:[UIImage imageNamed:@"BookmarkButtonSelected.png"] forState:UIControlStateSelected];

Does anyone have any idea how I can go about doing this?

Community
  • 1
  • 1
Jay
  • 299
  • 4
  • 13

1 Answers1

12

I found a solution to this by accident. It was this simple:

[self.searchBar setImage:[UIImage imageNamed:@"BookmarkButtonNormal.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
[self.searchBar setImage:[UIImage imageNamed:@"BookmarkButtonSelected.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateSelected];
Jay
  • 299
  • 4
  • 13
  • may no longer work. use [UISearchBar.appearance setImage:[UIImage imageNamed:@"YourImage"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal]; – Bill Bunting Jul 05 '19 at 19:11