iOS 添加 UISearchController 取消隐藏 NavigationBar
iOS adding a UISearchController is unhiding the NavigationBar
当我使用 UISearchController 更新我的 table 视图时,我遇到了这种奇怪的副作用(如果我从 table 视图中 select 某些东西而不搜索错误不会表现出来)。但是当我搜索时,select 一个单元格,然后由于某种原因 popViewControllerAnimated:
NavigationBar 不再隐藏。我想认为这是 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
我认为一旦您的搜索结果返回,您就是在重新加载主 table 而不是搜索 table。这就是您应该如何在搜索结果中加载数据 table。
self.searchController.searchResultsTableView reloadData];
如果您设置 searchController.hidesNavigationBarDuringPresentation = NO
,问题会消失吗?
可能发生了以下情况:
- 开始搜索时,
searchController.active
设置为 YES
。因此 searchController
调用 [... setNavigationBarHidden:YES]
因为默认情况下 UISearchController.hidesNavigationBarDuringPresentation = YES
。
popViewControllerAnimated:
被调用。
searchController.active
设置为 NO
,因此 searchController
调用 [... setNavigationBarHidden:NO]
。这会导致显示导航栏。
当我使用 UISearchController 更新我的 table 视图时,我遇到了这种奇怪的副作用(如果我从 table 视图中 select 某些东西而不搜索错误不会表现出来)。但是当我搜索时,select 一个单元格,然后由于某种原因 popViewControllerAnimated:
NavigationBar 不再隐藏。我想认为这是 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
我认为一旦您的搜索结果返回,您就是在重新加载主 table 而不是搜索 table。这就是您应该如何在搜索结果中加载数据 table。
self.searchController.searchResultsTableView reloadData];
如果您设置 searchController.hidesNavigationBarDuringPresentation = NO
,问题会消失吗?
可能发生了以下情况:
- 开始搜索时,
searchController.active
设置为YES
。因此searchController
调用[... setNavigationBarHidden:YES]
因为默认情况下UISearchController.hidesNavigationBarDuringPresentation = YES
。 popViewControllerAnimated:
被调用。searchController.active
设置为NO
,因此searchController
调用[... setNavigationBarHidden:NO]
。这会导致显示导航栏。