关于使用UISearchController遇到的坑

今天需要做一个搜索的功能,就想到用系统的UISearchController 感觉做起来还是不错的 首先

// 创建UISearchController, 这里使用当前控制器来展示结果
    UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil];
    // 设置结果更新代理
    search.searchResultsUpdater = self;
    // 因为在当前控制器展示结果, 所以不需要这个透明视图
    search.dimsBackgroundDuringPresentation = NO;
//    self.definesPresentationContext = YES;
    // 是否自动隐藏导航
//    search.hidesNavigationBarDuringPresentation = NO;
    //必须设置这个 如果不设置这个那么点击tableView
//    self.definesPresentationContext = YES;
    self.searchController = search;
    // 将searchBar赋值给tableView的tableHeaderView
    self.tableView.tableHeaderView = search.searchBar;
    search.searchBar.delegate = self;

一切做完了之后感觉效果还是可以的
Simulator Screen Shot - iPhone X - 2018-01-08 at 14.21.26.png

Simulator Screen Shot - iPhone X - 2018-01-08 at 14.21.30.png

可是当我搜索点击了之后 在回到我需要的界面的时候上面的 searchBar 还停留在上面, 明明不在一个界面了为啥要没有消失掉呢,使用离开了结算编辑的方法也不行, 当然还有一中方法就是你去调用点击"Cancel"的方法视乎也没有那么好做,

Simulator Screen Shot - iPhone X - 2018-01-08 at 14.20.32.png

之后查了一下资料需要在实例化的时候添加下面这句代码即可

self.definesPresentationContext = YES; 
Simulator Screen Shot - iPhone X - 2018-01-08 at 14.21.07.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容