在我的应用程序中,我有以下设置:
- UIViewController
- 作为 subview 的 UITableView
tableView 有一个 UISearchController 和一个搜索栏是一个表头 View
_tableView.tableHeaderView = _searchController.searchBar;
我想实现的是:
- 出现此屏幕时,搜索栏不存在
- 点击某个按钮(例如导航 Controller 右键)出现搜索栏(可能从顶部动画或其他)
- 再次点击该按钮搜索栏隐藏
当搜索栏没有显示表格时,看起来好像那个栏从来没有出现过(没有空的标题单元格或类似的东西)
非常感谢任何形式的帮助!
最佳答案
这是我自己想出来的,也许不是最佳解决方案,但如果有人有更好的想法,我会很乐意接受它作为答案。
-(void)searchButtonDidTap:(UIButton*)aButton
{
if (!_searchController){
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.delegate = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_searchController.active = NO;
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_tableView.tableHeaderView = _searchController.searchBar;
_searchController.searchBar.delegate = self;
}
else
{
_searchController.active = NO;
[_searchController removeFromParentViewController];
_searchController = nil;
_tableView.tableHeaderView = nil;
}
[_tableView reloadData];
}
关于ios - 如何在 UITableView 中显示/隐藏 UISearchController searchBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324864/