2013-01-08 43 views
1

我創建了一個UISearchBar控制與書籤圖標顯示在搜索字段本身。我現在正在嘗試尋找方法讓我用圖像來擦拭書籤按鈕。我偶然發現this post(參考第四條評論),它似乎能夠做我想做的事,但只適用於搜索字段中的x按鈕,而不是書籤按鈕。自定義書籤圖標添加到UISearchBar

我嘗試了以下行更改按鈕,但它似乎並沒有工作:

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

沒有人有任何想法,我怎麼能去這樣做?

回答

11

我偶然發現了一個解決方案。這是這麼簡單:

[self.searchBar setImage:[UIImage imageNamed:@"BookmarkButtonNormal.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal]; 
[self.searchBar setImage:[UIImage imageNamed:@"BookmarkButtonSelected.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateSelected];