我有 ViewController1
,它使用 segue 来显示 ViewController2
。它们都是 UINavigationController 的一部分。
我在 ViewController2
上有一个按钮,它应该隐藏状态栏。
我浏览了几个与此相关的 Stackoverflow 问题并发现了以下内容,但仍然无法使其正常工作:
在 Info.plist 中将“查看基于 Controller 的状态栏外观”设置为
true
在 ViewController2 中,我添加了一个方法
override func PreferredStatusBarHidden() -> Bool
,并返回true
或false
(基于按钮是否被点击)当单击按钮强制选择状态栏更改时,我会调用
setNeedsStatusBarAppearanceUpdate
。我读到了有关
modalPresentationCapturesStatusBarAppearance
的内容(需要启用它)。我尝试在 ViewController2 上将其设置为true
prepareForSegue
但是,状态栏仍然显示,并且从未调用prefersStatusBarHidden。
最佳答案
问题是您的 View Controller 位于 UINavigationController 界面中。因此,运行时会咨询导航 Controller 来了解状态栏,而不是 View Controller 。
但是,导航 Controller 又会咨询您的 View Controller ,因此只需输入 self.navigationController?.setNeedsStatusBarAppearanceUpdate()
即可让一切顺利进行。它对我来说效果很好:
关于ios - 在 iOS 9 的一个 View Controller 上隐藏状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671257/