Jay
Jay

Reputation: 299

Customising bookmark icon added to UISearchBar

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?

Upvotes: 2

Views: 3576

Answers (1)

Jay
Jay

Reputation: 299

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];

Upvotes: 12

Related Questions