gpt4 book ai didi

ios - UISearchDisplayController 无法隐藏导航栏

转载 作者:行者123 更新时间:2023-12-03 18:14:03 27 4
gpt4 key购买 nike

我正在处理带有搜索栏的 View 。当搜索激活时,导航栏应该动画化(即像 -[navigationController setNavigationBarHidden:animated:]

我已经使用 UISearchDisplayController 实现了这个,它大部分都做了正确的事情,除了它没有隐藏导航栏。 View 层次结构是这样堆叠的:

UIWindow
+-> IIViewDeckController
+->UINavigationController
+->UITableViewController

我已经初始化了这样的东西:

- (void)viewDidLoad
{
UISearchBar *searchBar = [[UISearchBar alloc] init];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.tableView.tableHeaderView = searchBar;
}

我也试过使用不同的抽屉 Controller 来查看问题是否出在 IIViewDeckController 中,如下所示:

UIWindow
+-> MMDrawerController
+->UINavigationController
+->UITableViewController

通常,UISearchDisplayController 应该从 -[UISearchDisplayController setActive:animated:] 的某处调用 -[UINavigationController setNavigationBarHidden:animated:]。我什至尝试创建一个派生自 UISearchDisplayController 的类并重载 -[UISearchDisplayController setActive:animated:] 并手动隐藏导航栏,如下所示:

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[self.searchContentsController.navigationController setNavigationBarHidden:visible animated:animated];
[super setActive:visible animated:animated];
}

为清楚起见进行更新:这段代码确实按预期隐藏了导航栏,但它并没有清除搜索栏以北的 View ,最后我在上面找到了另一个 View 状态栏(这有点有意义,因为在通过 -[UISearchDisplayController setActive:animated:] 进行调试时,我发现它在隐藏导航栏时使用一些私有(private) API 来清除刷新 Controller 。我认为。

出于某种原因,导航栏没有隐藏,我费了好大劲才弄清楚原因。

什么会导致这种行为?

更新 #2

看起来我错过了层次结构中的一个 View ,它似乎在某种程度上是罪魁祸首(仍然不确定究竟是什么方式)。在这里:

UIWindow
+-> MMDrawerController
+-> UINavigationController
+-> UITabBarController
+-> UITableViewController

最佳答案

看起来 UITabBarController 是问题所在,我早该知道,因为这里甚至提到了它 (How to combine TabBarController with Navigation Controller?) 并且它在文档中,although not too prominently:

An app that uses a tab bar controller can also use navigation controllers in one or more tabs. When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers.

在我看来,答案是 View 层次结构需要更改,这样就没有 UITabBarController(因为使用一个需要它成为根 Controller ,这将强制重新设计应用程序)。看起来最可行的替代方案是 use a UITabBar without a UITabBarController

关于ios - UISearchDisplayController 无法隐藏导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104035/

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