我创建了一个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];
有没有人知道我该怎么做呢?
答案 0 :(得分:13)
我意外地找到了解决方法。就是这么简单:
[self.searchBar setImage:[UIImage imageNamed:@"BookmarkButtonNormal.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
[self.searchBar setImage:[UIImage imageNamed:@"BookmarkButtonSelected.png"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateSelected];