UISearchController在推送时不会隐藏视图 [英] UISearchController doesn't hide view when pushed

查看:151
本文介绍了UISearchController在推送时不会隐藏视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using UISearchController to display a search bar and results within a UITableView. I managed to set it up correctly, but when I search the results and then select one of the rows in the tableview, and push a new view controller to the navigation stack, I would expect the search bar to not be visible anymore. However, when I try this, the search bar from the first view controller is visible in the 2nd view controller:

    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;
}

一个选项是调用 self.searchController setActive: NO] didSelectRowAtIndexPath:里面但没有办法在没有每次搜索时关闭搜索栏的分散动画的情况下这样做结果选自。

One option is to call self.searchController setActive:NO] inside of didSelectRowAtIndexPath: but there isn't a way to do this without the distracting animation of bringing down the search bar each time the search results are selected from.

有没有人遇到同样的问题?有没有办法告诉UISearchController在推送时隐藏搜索栏?当我使用UISearchDisplayController时它工作正常

Does anyone have the same problem? Is there a way to tell UISearchController to hide the search bar when pushed? It worked fine when I was using UISearchDisplayController

推荐答案

将它放在你的viewDidLoad中:

Put this in your viewDidLoad:

Swift:

self.definesPresentationContext = true

Objective-C:

Objective-C:

self.definesPresentationContext = YES;

这解决了我的问题。

这篇关于UISearchController在推送时不会隐藏视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆