关于使用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;

一切做完了之后感觉效果还是可以的
关于使用UISearchController遇到的坑_第1张图片
Simulator Screen Shot - iPhone X - 2018-01-08 at 14.21.26.png

关于使用UISearchController遇到的坑_第2张图片
Simulator Screen Shot - iPhone X - 2018-01-08 at 14.21.30.png

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

关于使用UISearchController遇到的坑_第3张图片
Simulator Screen Shot - iPhone X - 2018-01-08 at 14.20.32.png

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

self.definesPresentationContext = YES; 
关于使用UISearchController遇到的坑_第4张图片
Simulator Screen Shot - iPhone X - 2018-01-08 at 14.21.07.png

你可能感兴趣的:(关于使用UISearchController遇到的坑)