UISearchBar 修改背景和textField
UISearchBar 修改背景和textField
修改背景
1.修改背景
方法一:
for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
方法二:
for (UIView *subview in [[self.searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview setAlpha:0.0];
break;
}
}
2.设置背景图片
[searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"按钮"] forState:UIControlStateNormal];
[searchBar setBackgroundImage:[UIImage imageNamed:@"按钮2"]];
[searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"按钮1"] forState:UIControlStateHighlighted];
修改textField
1.获取
UITextField *textField = [searchBar valueForKey:@"_searchField"];
UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
//此处获取
break;
}
}`
2.修改搜索时的放大镜图标
结构图[searchBar setBackgroundImage:[[UIImage alloc] init] ];//if you want to remove background of uisearchbar
UIImage *image = [UIImage imageNamed: @"00b0f0”]; //00b0f0是自己设置的图标
[searchBar setImage:image forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];