搜索框UISearchController的使用及所遇到的坑
2018-01-22 15:21
459 查看
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也不例外。下面请看图:tableView整体左移.png
根据需求这个不是我们要的效果,那如何解决这个问题的,所以在网上找答案,刚开始用百度(没买翻墙软件,所以没到万不得已不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];效果图:
正常图.png
第二个坑 —— 搜索时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。如图:
搜索结果向上偏移20px.png
解决办法是:
-(void)viewDidLayoutSubviews { if(self.searchController.active) { [self.brandListTableView setFrame:CGRectMake(0, 20, TLScreenWidth, self.view.height -20)]; }else { self.brandListTableView.frame =self.view.bounds; } }解决之后:
搜索正常.png
这个问题解决之后,新问题又出来了,那就是,每次搜索完返回之后,tableView都向上偏移20(每次累加),如图:
tableView向上偏移20px.png
解决办法是,让控制器不自动滚动:
self.automaticallyAdjustsScrollViewInsets = NO;以上就是我这次项目中使用的UISearchController和遇到的坑,欢迎交流,如有什么写的不好的,请多多指教!
作者:TongRy
链接:https://www.jianshu.com/p/9ab34ab26227
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章推荐
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)
- 关于UISearchDisplayController和 uisearchBar使用时遇到的问题
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
- UI_搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
- iOS - 搜索框UISearchController的使用(iOS8.0之后替代UISearchBar + UISearchDisplayController的组合)
- iOS 当使用FD_FullscreenPopViewController的时候遇到scrollView右滑手势无法使用的解决
- 搜索框(UISearchController),最近踩过的坑
- iOS UISearchController的使用
- UISearchBar和 UISearchDisplayController的使用
- iOS UISearchController的使用(iOS 8.0以上)
- iOS - Swift UISearchController仿微信搜索框
- iOS开发实战之搜索控制器UISearchController使用
- UISearchBar 和 UISearchDisplayController的使用
- [iOS学习]之UISearchController简单使用
- 关于UISearchController的使用方法
- iOS UISearchController使用
- 关于SpringMVC4使用过程中的路径问题,我们经常遇到的Controller未执行以及执行了之后出现404
- iOS中UISearchBar(搜索框)使用总结
- UISearchController使用