iOS在搜索栏中更改图标
答
Open-source是你的朋友(就像可可,呵呵):
- (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];
答
searchBar.setImage(UIImage(named: "tempUser")!,
forSearchBarIcon: UISearchBarIcon.Search,
state: UIControlState.Normal)
+0
Swift 3:'searchBar.setImage(UIImage(named:“tempUser”),用于:.search,state:.normal )' – 2017-11-22 21:57:19
完美!这正是我所期待的。非常感谢你!!! – KevinM 2012-03-20 21:29:23
在iOS 7中,你应该为'([[searchBar.subviews objectAtIndex:0]子视图])中的'for(UIView * subview)',因为现在有中间视图 – 2013-12-25 16:05:52
@Aurelien Porte,从iOs 5你应该使用官方方法:' - (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)图标状态:(UIControlState)状态'它不适合你吗? – 2013-12-26 10:26:51