为什么hidesBottomBarWhenPush在不是root视图控制器时不工作?

时间:2017-04-15 12:45:50

标签: swift uinavigationcontroller pushviewcontroller

按下视图控制器,隐藏底部标签栏,如下所示:

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被忽略一样。

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

解决方案是不更改根视图控制器,而只是将视图添加到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()