![](/img/trans.png)
[英]iOS 13 How to hide UISearchController searchBar in navigationBar
[英]iOS adding a UISearchController is unhiding the NavigationBar
當我使用 UISearchController 更新我的表格視圖時,我遇到了這個奇怪的副作用(如果我從表格視圖中選擇了一些東西而不搜索錯誤不會自己表現出來)。 但是當我搜索時,選擇一個單元格,然后popViewControllerAnimated:
由於某種原因導航欄不再隱藏。 我想認為這是 iOS 中的一個錯誤,而不是特定於我的代碼。 但我想我會看看是否有人能發現我的代碼中的錯誤,或者對我可能做錯的事情有任何想法。 我添加了[self.navigationController setNavigationBarHidden:YES];
到我的rootView
viewWillAppear
,但在動畫結束之前欄不會消失。
我的 TableView/UISearchController 代碼:
@interface LBSelectUniversityView()<UISearchResultsUpdating, UISearchBarDelegate>
@property (strong, nonatomic) UISearchController *searchController;
@end
@implementation LBSelectUniversityView {
NSArray *schoolNames;
NSArray *searchResults;
}
- (void)viewDidLoad {
[super viewDidLoad];
schoolNames = [[LBUtilities sharedInstance] schoolNames];
searchResults = schoolNames;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return searchResults.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
...
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText{
if ([searchText isEqualToString:@""]) return;
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [schoolNames filteredArrayUsingPredicate:resultPredicate];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = searchController.searchBar.text;
[self filterContentForSearchText:searchString];
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
[self.navigationController popViewControllerAnimated:YES];
}
@end
如果您設置searchController.hidesNavigationBarDuringPresentation = NO
問題會消失嗎?
也許正在發生以下情況:
searchController.active
設置為YES
。 因此searchController
調用[... setNavigationBarHidden:YES]
因為UISearchController.hidesNavigationBarDuringPresentation = YES
默認情況下。popViewControllerAnimated:
被調用。searchController.active
設置為NO
,因此searchController
調用[... setNavigationBarHidden:NO]
。 這會導致顯示導航欄。我認為一旦您的搜索結果回來,您正在重新加載您的主表而不是搜索表。 這是您應該如何在搜索結果表上加載數據。
self.searchController.searchResultsTableView reloadData];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.