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

 
 

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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值