如果直接采用
self.searchBar.backgroundColor = [UIColor redColor];
self.searchBar.tintColor = [UIColor redColor];
可能会达不到预想的效果,所以就得采用其他方案了。
先看UISearchBar的层级结构,SearchBar里面包含的view下有三个控件,分别是“UIImageView类型的UISearchBarBackgroud”、“UITextField类型的UISearchBarTextField”、“UIButton类型UINavigationButton”,所以就可以采用遍历循环获取到UISearchBar内层的视图,依次进行修改
UISearchBar的层级结构 代码for (UIView *view in self.searchController.searchBar.subviews.lastObject.subviews) {
if([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
UIImageView*viewSearch = (UIImageView*)view;
viewSearch.image= [UIImagecreateImageWithColor:[UIColorwhiteColor]];
}else if ([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]){
UITextField*textField = (UITextField*)view;
textField.backgroundColor= UIColorFromRGB(0xF5F5F5);
}else if ([view isKindOfClass:NSClassFromString(@"UINavigationButton")]){
UIBotton*botton = (UIBotton*)view;
}
}
网友评论