简体   繁体   中英

Hide statusbar on one viewcontroller for iOS 9

I have ViewController1 which uses segue to show ViewController2 . Both of them are part of UINavigationController.

I have a button on ViewController2 which should hide the statusbar.

I went through several Stackoverflow questions regarding this and found following, but still can't get it working:

  • In Info.plist set "View controller-based status bar appearance" to true

  • In ViewController2 I added a method override func prefersStatusBarHidden() -> Bool and I return true or false (based on whether button was clicked or not)

  • I call setNeedsStatusBarAppearanceUpdate when a button clicked to force statusbar change be picked up.

  • I read about modalPresentationCapturesStatusBarAppearance (that it needs to be enabled). I tried to set it to true prepareForSegue on ViewController2

However, the status bar is still shown and prefersStatusBarHidden was never called.

The problem is that your view controller is in a UINavigationController interface. Thus it is the navigation controller that is consulted by the runtime about the status bar, not your view controller.

However, the navigation controller in its turn will consult your view controller, so it should be sufficient to say self.navigationController?.setNeedsStatusBarAppearanceUpdate() to set the ball rolling. It works fine for me:

在此处输入图片说明

Thanks to matt's answer and Chris Hulbert's article about StatusBar and this SO question .

Apparently, you need to make sure that everybody in your ViewController hierarchy forward prefersStatusBarHidden to appropriate child.

There are some iOS controllers which do that (as example UINavigationViewController) and there are some which don't (they don't know to which one they should forward it). If you have any of them (as example UISplitViewController) then you need to subclass it and override childViewControllerForStatusBarHidden and write a code to forward it to appropriate child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM