44

I would like to show and hide the Status bar on some controllers. Can this be done or is it more of an overall app setting.

I have seen many posts/questions about the plist update:

View controller-based status bar appearance - NO

If this is completed what control is then given?

I am looking to show the status bar on the main screen of the application. But for example on a side (slide) menu I would like it not to show, is this possible? Can this be changed in IB or code?

EDIT -- I am using a https://github.com/edgecase/ECSlidingViewController implementation.

The main controller (showing the first page) should show the Status bar, but the left menu controller when it slides should not.

I believe the issue is that they both sit within the same root controller (sliding view controller) so it is difficult to complete.

Ideally if the home screen (main page) could take the status bar with it when it slides that would look best.

4

5 回答 5

129

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;
}
于 2013-09-20T11:36:29.570 回答
14

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.

于 2015-09-22T23:24:21.910 回答
14

Swift 3:

override var prefersStatusBarHidden: Bool {
    return true
}
于 2016-10-25T11:28:43.470 回答
6

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
}
于 2016-12-29T19:06:13.163 回答
2

Swift version of Mojo66's answer:

override func prefersStatusBarHidden() -> Bool {
    return true
}
于 2016-02-27T21:06:28.857 回答