自定义添加到UISearchBar的书签图标

时间:2013-01-08 13:40:36

标签: iphone xcode cocoa-touch ios6 uisearchbar

我创建了一个UISearchBar控件,其中书签图标显示在搜索字段中。我现在正试图寻找方法让我用图像为书签按钮设置外观。我偶然发现this post(参见第四条评论)似乎可以做我想做的事情,但只适用于搜索字段中的x按钮,而不适用于书签按钮。< / p>

我尝试了以下行来更改按钮,但它似乎不起作用:

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

有没有人知道我该怎么做呢?

1 个答案:

答案 0 :(得分:13)

我意外地找到了解决方法。就是这么简单:

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