我正在尝试自定义搜索栏。我已经想出如何更改搜索栏中的背景和文本框,但我找不到更改栏中默认图标(放大镜图标)的方法。
有办法吗?
最佳答案
Open-source是你的 friend (就像 cocoa 一样,呵呵):
- (void)setSearchIconToFavicon {
// Really a UISearchBarTextField, but the header is private.
UITextField *searchField = nil;
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
UIImage *image = [UIImage imageNamed: @"favicon.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
searchField.leftView = iView;
[iView release];
}
}
从 iOS 5.0 开始:
[self.testSearchBar setImage:[UIImage imageNamed: @"favicon.png"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
关于iOS 更改搜索栏中的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794552/