美文网首页
关于使用UISearchController遇到的坑

关于使用UISearchController遇到的坑

作者: 我太难了_9527 | 来源:发表于2018-01-08 14:33 被阅读0次

今天需要做一个搜索的功能,就想到用系统的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

相关文章

网友评论

      本文标题:关于使用UISearchController遇到的坑

      本文链接:https://www.haomeiwen.com/subject/dtrbnxtx.html