为什么 hidesBottomBarWhenPushed 在不是根视图控制器时不起作用?

Why is hidesBottomBarWhenPushed not working when not root view controller?

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

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()