搜索框UISearchController的使用及所遇到的坑

以前没遇到过搜索功能,所以一直也没做,这次项目中用到了,就查了下资料,因为我们项目支持iOS8以上的设备,所以就采用的UISearchController,然后就愉快的用了起来,可是不用不知道,一用吓一跳呀,刚开始写的demo,没有navigation,也没有sectionIndex,感觉一点坑都没有,简直很爽,殊不知无数的坑在等着我跳。下面我就讲讲我的使用及我所遇到的坑。
UISearchController的使用
使用懒加载:
- (UISearchController *)searchController {
if(!_searchController) {
UISearchController *searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController = searchVC;
//取消蒙版
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchResultsUpdater = self;
_searchController.searchBar.placeholder = @"搜索";
//改变searchBar的文字
[_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
//改变取消按钮字体颜色
self.searchController.searchBar.tintColor = [UIColor colorWithRed:239 / 255.0 green:128 / 255.0 blue:25 / 255.0 alpha:1];
//去掉searchController.searchBar的上下边框(黑线)
UIImageView *barImageView = [[[_searchController.searchBar.subviews firstObject] subviews] firstObject];
barImageView.layer.borderColor = UIColorFromRGB(0xe5e5e5).CGColor;
barImageView.layer.borderWidth = 1;
//改变searchController背景颜色
_searchController.searchBar.barTintColor = UIColorFromRGB(0xe5e5e5);
//此处重要一步,将searbar显示到界面上(在tableView头视图中添加时searbar有时会消失,不可控制,原因未找到)
[self.view addSubview:_searchController.searchBar];
}
return _searchController;
}
第一个大坑 —— sectionIndex 和 searchBar冲突
searchBar加到tableView的headerView上,然后为tableView添加sectionIndex,问题就出来了,因为sectionIndex会占用位置,tableView整体左移,searchBar也不例外。下面请看图:

根据需求这个不是我们要的效果,那如何解决这个问题的,所以在网上找答案,刚开始用百度(没买翻墙软件,所以没到万不得已不Google),说把sectionIndex背景清除就可以,可是并不能。
[self.brandListTableView setSectionIndexBackgroundColor:[UIColor clearColor]];
然后在百度找了好几页也没找到满意的答案,最后在google里面找到了,解决方法是把searchBar加在一个UIView上,然后把UIView加在tableHeaderView上,同时sectionIndex背景色要清除,也就是上面一段代码(因为sectionIndex一直浮在最上层),就完美解决问题。
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _brandListTableView.bounds.size.width, _searchController.searchBar.size.height)];
[headerView addSubview:self.searchController.searchBar];
//设置tableHeaderView
[self.brandListTableView setTableHeaderView:headerView];
效果图:

第二个坑 —— 搜索时searchBar向上偏移64
当在有navigation的时候点击,搜索时,searchBar向上偏移64,网上的解决方案是:
self.definesPresentationContext = YES;
给出的解释是:
设置definesPresentationContext为YES,可以保证在UISearchController在激活状态下用户push到下一个view controller之后search bar不会仍留在界面上。
苹果对它官方的解释是// know where you want UISearchController to be displayed
a、如果不添加上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,搜索框进入编辑模式会导致,searchbar不可见,偏移-64;
在设置为NO的时候,进入编辑模式输入内容会导致高度为64的白条,猜测是导航栏没有渲染出来
b、如果添加了上面这行代码,在设置hidesNavigationBarDuringPresentation这个属性为YES的时候,输入框进入编辑模式正常显示和使用;在设置为NO的时候,搜索框进入编辑模式导致向下偏移64,具体原因暂时未找到
第三个坑 —— 搜索结果tableView向上偏移20px
上面的问题解决之后,新问题又出现了,那就是搜索出结果后,tableView会向上偏移20px。
如图:

解决办法是:
-(void)viewDidLayoutSubviews {
if(self.searchController.active) {
[self.brandListTableView setFrame:CGRectMake(0, 20, TLScreenWidth, self.view.height -20)];
}else {
self.brandListTableView.frame =self.view.bounds;
}
}
解决之后:

这个问题解决之后,新问题又出来了,那就是,每次搜索完返回之后,tableView都向上偏移20(每次累加),如图:

解决办法是,让控制器不自动滚动:
self.automaticallyAdjustsScrollViewInsets = NO;
以上就是我这次项目中使用的UISearchController和遇到的坑,欢迎交流,如有什么写的不好的,请多多指教!