gpt4 book ai didi

iOS 添加 UISearchController 取消隐藏 NavigationBar

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:17 25 4
gpt4 key购买 nike

当我使用 UISearchController 更新我的 TableView 时,我遇到了这种奇怪的副作用(如果我在没有搜索的情况下从 TableView 中选择了一些东西,这个错误不会自行显现)。但是当我搜索时,选择一个单元格,然后 popViewControllerAnimated: 由于某种原因 NavigationBar 不再隐藏。我想认为这是 iOS 中的一个错误,而不是我的代码所特有的。但我想我会看看是否有人可以发现我的代码中的错误或对我可能做错的事情有任何想法。我已经添加了 [self.navigationController setNavigationBarHidden:YES]; 到我的 rootViewviewWillAppear 但栏不会消失,直到动画结束。

我的 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,问题会消失吗?

可能会发生以下情况:

  1. 开始搜索时,searchController.active 设置为 YES。因此 searchController 调用 [... setNavigationBarHidden:YES] 因为默认情况下 UISearchController.hidesNavigationBarDuringPresentation = YES
  2. popViewControllerAnimated: 被调用。
  3. searchController.active 设置为 NO,因此 searchController 调用 [... setNavigationBarHidden:NO] .这会导致显示导航栏。

关于iOS 添加 UISearchController 取消隐藏 NavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32803892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com