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];

结构图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容