[英]iOS 13 How to hide UISearchController searchBar in navigationBar
[英]How to show/hide UISearchController searchBar in UITableView?
在我的應用中,我具有以下設置:
tableView有一個UISearchController,搜索欄是一個表頭視圖
_tableView.tableHeaderView = _searchController.searchBar;
我想要實現的是:
當搜索欄未顯示時,表格看起來好像從未出現過該欄(沒有空的標題單元格或類似的內容)
任何幫助都將受到高度贊賞!
我自己想了這個,也許不是最理想的解決方案,但是如果有人有更好的主意,我會很樂意接受。
-(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];
}
您可以通過多種方式執行此操作,但是我會執行以下操作:
更新viewForHeaderInSection方法以僅在名為_showHeader的新類屬性為true時顯示標頭:
if(_showHeader) { // insert code for showing the header: // return HeaderCell; } else { return nil; }
當用戶單擊按鈕以顯示標題時,設置一些變量並在tableView上調用reloadData:
_showHeader = YES; [_tableView reloadData];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.