iOS 7 - Hide status bar on a child view controller

2019-05-07 14:41发布

问题:

UIApplication.SharedApplication.SetStatusBarHidden(MonoTouch code, but also applies to Obj-C) does the job for iOS 6, but for iOS 7 we must:

  1. Invoke SetNeedsStatusBarAppearanceUpdate () on the view controller (e.g.: a view controller that is showing a fullscreen image)
  2. Override PrefersStatusBarHidden () on the same view controller.

However the view controller mentioned above is embedded in a navigation controller, which is also embedded in a slideout menu. Even though all embedded view controllers are added to the hierarchy using AddChildViewController(), attempting to update the status bar in a child view controller has no effect.

Any ideas?


EDIT

Window.RootViewController (Menu)
|___ ViewControllerA (Navigation Bar)
     |___ ViewControllerB (Actual View controller)

Where ViewControllerB wants to hide/show the status bar

回答1:

ViewControllerA should override childViewControllerForStatusBarHidden and return ViewControllerB.

- (UIViewController *)childViewControllerForStatusBarHidden {
    return _viewControllerB;
}


回答2:

It does not appear entirely what you attempt to do. If you simple want to hide the statusbar in all child viewcontrollers, you could set the new plist propertyUIViewControllerBasedStatusBarAppearance.

... If you prefer to opt out of this behavior and set the status bar style by using the UIApplication statusBarStyle method, add the UIViewControllerBasedStatusBarAppearance key to an app’s Info.plist file and give it the value NO.

source:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW1

The source is BTW a really good reference to all kinds of information related to the handling of UINavigationBar and UIStatusBar in iOS7.