推送时 UISearchController 不隐藏视图

UISearchController doesn't hide view when pushed

我正在使用 UISearchController 在 UITableView 中显示搜索栏和结果。我设法正确设置了它,但是当我搜索结果然后 select 表视图中的其中一行,并将新的视图控制器推送到导航堆栈时,我希望搜索栏不可见了。但是,当我尝试这样做时,第一个视图控制器的搜索栏在第二个视图控制器中可见:

    if (self.searchController == nil) {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.scopeButtonTitles = @[];
    self.searchController.searchBar.delegate = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;
}

一种选择是在 didSelectRowAtIndexPath: 内部调用 self.searchController setActive:NO],但如果没有每次搜索结果出现时下拉搜索栏的令人分心的动画,就无法做到这一点 select来自.

有没有人遇到同样的问题?有没有办法告诉 UISearchController 在按下时隐藏搜索栏?当我使用 UISearchDisplayController

时它工作正常

将此放入您的调用者的 viewDidLoad:

Swift:

self.definesPresentationContext = true

Objective-C:

self.definesPresentationContext = YES;

这解决了我的问题。