ios - 在 iOS 9 的一个 View Controller 上隐藏状态栏

标签 ios user-interface ios9 statusbar

我有 ViewController1,它使用 segue 来显示 ViewController2。它们都是 UINavigationController 的一部分。

我在 ViewController2 上有一个按钮,它应该隐藏状态栏。

我浏览了几个与此相关的 Stackoverflow 问题并发现了以下内容,但仍然无法使其正常工作:

  • 在 Info.plist 中将“查看基于 Controller 的状态栏外观”设置为 true

  • 在 ViewController2 中,我添加了一个方法 override func PreferredStatusBarHidden() -> Bool ,并返回 truefalse (基于按钮是否被点击)

  • 当单击按钮强制选择状态栏更改时,我会调用 setNeedsStatusBarAppearanceUpdate

  • 我读到了有关 modalPresentationCapturesStatusBarAppearance 的内容(需要启用它)。我尝试在 ViewController2 上将其设置为 true prepareForSegue

但是,状态栏仍然显示,并且从未调用prefersStatusBarHidden。

最佳答案

问题是您的 View Controller 位于 UINavigationController 界面中。因此,运行时会咨询导航 Controller 来了解状态栏,而不是 View Controller 。

但是,导航 Controller 又会咨询您的 View Controller ,因此只需输入 self.navigationController?.setNeedsStatusBarAppearanceUpdate() 即可让一切顺利进行。它对我来说效果很好:

enter image description here

关于ios - 在 iOS 9 的一个 View Controller 上隐藏状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671257/

相关文章:

iphone - 动画持续时间 : corrupts smooth progressbar increment

ios - 找不到 UIKit.framework

iOS SnapKit 和场景

ios - 仅在 iOS 9 上,触摸事件在屏幕左边缘附近延迟。如何解决?

ios - Div 向下移动到页面底部而不是 iOS 中屏幕高度的按钮

javascript - 获得焦点时选择文本框的所有内容(Vanilla JS 或 jQuery)

ios - 在 ios 中为 launchscreen.xib 设置背景图片

javascript - Accordion 和标签之间的区别

ios - 在 iOS 9.0.2 的 Webview 中播放 youtube 视频

ios - FBSDK 4.7.1登录时"You have already authorized yourApp"