今天需要做一个搜索的功能,就想到用系统的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;
一切做完了之后感觉效果还是可以的


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

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

网友评论