按下视图控制器并隐藏底部选项卡栏,如下所示:
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: MyViewController) as! MyViewController
myViewController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myViewController, animated: true)
那很好。
但是,当我在按下前更改根视图控制器时,底部栏不会隐藏。
// Change the root view controller
let firstRootViewController = UIApplication.shared.keyWindow!.rootViewController
UIApplication.shared.keyWindow!.rootViewController = secondRootViewController
// Push view on stack of navigation controller which is a child of firstRootViewController
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: MyViewController) as! MyViewController
myViewController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myViewController, animated: true)
// Some more things happen...
// Switch back to previous root view controller
UIApplication.shared.keyWindow!.rootViewController = firstRootViewController
结果是导航控制器正确地按下了
myViewController
,但底部的条是可见的,好像忽略了参数hidesBottomBarWhenPushed
。这里怎么了?
最佳答案
解决方案是不更改根视图控制器,而只将视图添加到keyWindow:
// Add another view on top of all views
UIApplication.shared.keyWindow?.addSubView(self.view)
// Push view on stack of navigation controller which is a child of firstRootViewController
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: MyViewController) as! MyViewController
myViewController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myViewController, animated: true)
// Some more things happen...
// Remove topmost view
self.view.removeFromSuperview()
关于swift - 为什么在没有root View Controller 的情况下hidesBottomBarWhenPushed不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43426238/