【发布时间】:2020-01-22 13:37:59
【问题描述】:
我在使用 hidesBottomBarWhenPushed 时遇到了麻烦... 我将按顺序将三个控制器 A、B 和 C 推送到导航控制器中,并且我想在显示 B 时隐藏底部标签栏。(并且 A 是标签栏控制器之一)
有人有想法吗?
【问题讨论】:
我在使用 hidesBottomBarWhenPushed 时遇到了麻烦... 我将按顺序将三个控制器 A、B 和 C 推送到导航控制器中,并且我想在显示 B 时隐藏底部标签栏。(并且 A 是标签栏控制器之一)
有人有想法吗?
【问题讨论】:
在视图控制器 A(位于 tabBar 上)中,当需要呈现 B(不需要 tabBar)时:
self.hidesBottomBarWhenPushed = YES; // hide the tabBar when pushing B
[self.navigationController pushViewController:viewController_B animated:YES];
self.hidesBottomBarWhenPushed = NO; // for when coming Back to A
在视图控制器 B 中,当需要呈现 C 时(再次需要 tabBar):
self.hidesBottomBarWhenPushed = NO; // show the tabbar when pushing C
[self.navigationController pushViewController:viewController_C animated:YES];
self.hidesBottomBarWhenPushed = YES; // for when coming Back to B
【讨论】:
hidesBottomBarWhenPushed 属性。您应该在特定的 viewController 上设置所需的值。在这种情况下,B 应将其设置为 YES,A 和 C 应将其设置为 NO。并确保您在 init 方法中执行此操作。
我没有在 viewDidLoad 中设置它,而是发现有时为时已晚。在 init 中设置它或覆盖 hidesBottomBarWhenPushed 以对没有底部工具栏的视图返回 YES。
【讨论】:
来自 hidesBottomBarWhenPushed 文档:
如果是,底部栏保持隐藏,直到视图控制器 从堆栈中弹出。
这意味着如果您不一定知道视图控制器将被推送的顺序,则需要堆栈中的所有视图控制器将其 hidesBottomBarWhenPushed 设置为 false,除了 topViewController。
那我该怎么办
这是 1 和 2 的一些代码)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.hidesBottomBarWhenPushed = false
if (segue.identifier == "MyViewControllerWhoHidesTabBar") {
let viewController: MyViewControllerWhoShowsTabBar = segue.destinationViewController as! MyViewControllerWhoShowsTabBar
viewController.hidesBottomBarWhenPushed = true
}
// rest of implementation....
}
3) 我已将后退按钮操作覆盖为
func backButtonClick(sender:UIButton!) {
let viewControllers = self.navigationController!.viewControllers
if let vc = viewControllers[viewControllers.count-2] as? MyViewController {
if vc.isKindOfPageYouDontWannaShowTheTabBar() == true {
vc.hidesBottomBarWhenPushed = true
}
}
navigationController?.popViewControllerAnimated(true)
}
【讨论】: