prefersStatusBarHidden 没有被调用

jrc*_*jrc 4 uinavigationcontroller uisplitviewcontroller ios

我有一个标准的主从应用程序,我正在尝试有条件地显示/隐藏状态栏。

prefersStatusBarHidden()在 MasterViewController 中覆盖什么都不做。它甚至永远不会被调用。

override func prefersStatusBarHidden() -> Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

UIViewControllerBasedStatusBarAppearanceInfo.plist 中的设置没有帮助,大概是因为YES已经是默认值。打电话setNeedsStatusBarAppearanceUpdate()也没有用。

我的目标是 iOS 9。

Vic*_*nin 5

有一个更清洁的解决方案。有一个函数childViewControllerForStatusBarHidden专门设计用于返回应将首选状态栏隐藏转发到的子视图控制器。

因此,最好覆盖它。它看起来像这样:

override func childViewControllerForStatusBarHidden() -> UIViewController? {
    if var topViewController = self.viewControllers.first {
        if let navigationController = topViewController as? UINavigationController {
            topViewController = navigationController.topViewController!
        }
        return topViewController
    }

    return super.childViewControllerForStatusBarHidden()
}
Run Code Online (Sandbox Code Playgroud)

也许你甚至可以省略跟随。NavigationViewController 本身有 childViewControllerForStatusBarHidden(),它将把它发送到子视图控制器。

  if let navigationController = topViewController as? UINavigationController {
      topViewController = navigationController.topViewController!
  }
Run Code Online (Sandbox Code Playgroud)