展开以查看搜索结果控制器活动在顶部留下黑色间隙

Unwinding to view with search results controller active leaves black gap at top

我有多个视图控制器,它们都使用一个 class(我们称它为 searchClass <NSObject>)我写道,它有一个 UISearchController 用于搜索外部API。用户可以点击搜索图标,搜索控制器变为活动状态:[_searchClass.searchController setActive:YES]; 它使用自己的 table 视图控制器(不是每个视图控制器中的那个,因为它们不都是 table 查看控​​制器)。

在我的例子中,我将搜索栏显示在导航栏中。搜索效果很好,用户可以 select 搜索结果并点击它以查看详细信息。 问题 是当用户从详细信息视图返回(展开)到搜索结果时,table 视图上方会短暂出现一个大约 44 磅高的黑色间隙和导航栏下方,然后消失。

这是我的设置。在视图控制器中:

    self.definesPresentationContext = YES;
    _searchClass = [SearchClass new];
    _searchClass.navController = [self navigationController];

在搜索class中:

    _searchTableViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"searchTableViewController"];
    _searchTableViewController.tableView.delegate = self;
    _searchTableViewController.tableView.dataSource = self;
    _searchTableViewController.tableView.backgroundColor = [UIColor clearColor];
    _searchTableViewController.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _searchTableViewController.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    _searchTableViewController.definesPresentationContext = YES;

    _searchController = [[UISearchController alloc] initWithSearchResultsController:_searchTableViewController];
    _searchController.delegate = self;
    _searchController.hidesNavigationBarDuringPresentation = NO;

    _searchController.searchBar.delegate = self;
    _searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    _searchController.searchBar.showsCancelButton = YES;
    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);

还有UISearchBarUISearchController常用的委托方法,以及显示搜索栏的代码,用动画替换了导航栏的titleView。

返回搜索结果后,如何消除差距?

我找到了解决方案,它与 UINavigationBar 有关。出于某种原因,将半透明 属性 设置为 NO 似乎导致了此问题。

self.navigationBar.translucent = YES;

将其设置为 YES(或省略该行,因为它是默认设置)使其正常工作。因为我仍然想让导航栏不透明,所以我做了以下操作使导航栏仅在过渡期间半透明,这似乎是问题所在:

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

-(void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

对我来说绝对像是一个真正的 iOS 错误。