IOS7 Status bar hide/show on select controllers

▼魔方 西西 提交于 2019-11-27 06:48:23

The plist setting "View controller-based status bar appearance" only controls if a per-controller based setting should be applied on iOS 7.

If you set this plist option to NO, you have to manually enable and disable the status bar like (as it was until iOS 6):

[[UIApplication sharedApplication] setStatusBarHidden:YES]

If you set this plist option to YES, you can add this method to each of your viewControllers to set the statusBar independently for each controller (which is esp. nice if you have a smart subclass system of viewControllers)

- (BOOL)prefersStatusBarHidden {
    return YES;
}

Edit:

there are two more methods that are of interest if you are opting in the new viewController-based status bar appearance -

Force a statusbar update with:

[self setNeedsStatusBarAppearanceUpdate]

If you have nested controllers (e.g. a contentViewController in a TabBarController subclass, your TabBarController subclass might ask it's current childViewController and forward this setting. I think in your specific case that might be of use:

- (UIViewController *)childViewControllerForStatusBarHidden {
     return _myChildViewController;
}
- (UIViewController *)childViewControllerForStatusBarStyle {
     return _myOtherViewController;
}

On iOS 7 and later, just implement -prefersStatusBarHidden, for example in a UIViewController that should hide the status bar:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

The default is NO.

Danut Pralea

Swift 3:

override var prefersStatusBarHidden: Bool {
    return true
}

You can also show/hide the status bar in an animation block, by putting animation code inside didSet property of variable that describes whether it should be shown or hidden. When you set a new value for the statusBarHidden Bool, this automatically triggers the animated updating of the status bar over the duration you have chosen.

/// Swift 3 syntax: 

var statusBarHidden: Bool = true {
    didSet {
        UIView.animate(withDuration: 0.5) { () -> Void in
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
}

override var prefersStatusBarHidden: Bool {
    return statusBarHidden
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)        
    statusBarHidden = false // show statusBar, animated, by triggering didSet block
}
James

Swift version of Mojo66's answer:

override func prefersStatusBarHidden() -> Bool {
    return true
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!