在使用那个UISearchBar的时候我个人觉得那个灰色背景框和背景很不搭。结果设置背景为无色,但是设置完了还是不行。然后我去网上看了一下,看到有两种解决的方法。
1.设置barTinColor和背景相同就看不出来那个灰色框了。因为肉眼看不出来所以这是一个障眼法。不建议大家使用哈。当然你要是觉得其他方法麻烦呢用这个还是可以的啦。
2.至于这种呢就取巧于那个subviews的方法了。这个方法跟我们的版本有关的,所以最先得判断版本了。iOS7.0以上有两层的subviews,以下有一层。刚刚好7.0呢就没有你可以直接设置背景颜色和那个barTinColor味无色就好了。接下来看代码
有两种写法。
(1)
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version == 7.0) {
_searchBar.backgroundColor = [UIColor clearColor];
_searchBar.barTintColor = [UIColor clearColor];
}else{
for(int i = 0 ;i < _searchBar.subviews.count;i++){
UIView * backView = _searchBar.subviews[i];
if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
[backView removeFromSuperview];
[_searchBar setBackgroundColor:[UIColor clearColor]];
break;
}else{
NSArray * arr = _searchBar.subviews[i].subviews;
for(int j = 0;j<arr.count;j++ ){
UIView * barView = arr[i];
if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
[barView removeFromSuperview];
[_searchBar setBackgroundColor:[UIColor clearColor]];
break;
}
}
}
}
}
(2)
// 去掉UISearchBar的那个灰色背景框
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if ([self.searchController.searchBar respondsToSelector:@selector(barTintColor)]) {
float iosversion7_1 = 7.1;
if (version >= iosversion7_1){
[[[[self.searchController.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
[self.searchController.searchBar setBackgroundColor:[UIColor clearColor]];
}else { //iOS7.0
[self.searchController.searchBar setBarTintColor:[UIColor clearColor]];
[self.searchController.searchBar setBackgroundColor:[UIColor clearColor]];
}
}else {
//iOS7.0以下
[[self.searchController.searchBar.subviews objectAtIndex:0] removeFromSuperview];
[self.searchController.searchBar setBackgroundColor:[UIColor clearColor]];
}
// 禁止搜索栏上移
self.searchController.hidesNavigationBarDuringPresentation = NO;