IOS7 Status bar hide/show on select controllers

58,575

Solution 1

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;
}

Solution 2

Swift 3:

override var prefersStatusBarHidden: Bool {
    return true
}

Solution 3

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.

Solution 4

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
}

Solution 5

Swift version of Mojo66's answer:

override func prefersStatusBarHidden() -> Bool {
    return true
}
Share:
58,575

Related videos on Youtube

StuartM
Author by

StuartM

Web Developer/iOS Developer. Current Apps: Where to Next -https://itunes.apple.com/us/app/where-to-next/id884766874?ls=1&mt=8 Shows business data around your current location.

Updated on December 30, 2020

Comments

  • StuartM
    StuartM over 3 years

    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.

  • StuartM
    StuartM almost 11 years
    Thanks, I think the issue is with the use of the sliding view controller. As the implementation uses a root view controller, then loads a view controller into the middle, and one under for the left. This means they are both controlled by the root view controller therefore setting the status bar to show/hidden happens on both.
  • auco
    auco almost 11 years
    It's clearly an issue of the ECSSlidingViewController you are using. But you know, that you don't need any third party sliding view controllers anymore on iOS7? However, if implemented correctly, it should work by setting the info.plist statement to NO and using -prefersStatusBarHidden.
  • StuartM
    StuartM almost 11 years
    Right, we are supporting IOS6 too though so we cannot use the ios7 functionality really. I guess this is not something I can change or a workaround too. As there is really one root view controller holding two controllers I cannot have alternate. thanks
  • auco
    auco almost 11 years
    I haven't looked at the ECSSliding code, but so far I've only seen horrible solutions to this kind of nicetohave design feature. It violates so many programming design principles to have two view controllers in one view just to have a nice interactive transition effect. If it were my app, I'd cut down this feature for iOS 6 users (and use a normal non-interctive UINavigationController leftToRight transition), because iOS 7 has a beautiful new interactive transition that does exactly what you are looking for: See session 218 "Custom Transitions Using View Controllers" of the WWDC videos.
  • StuartM
    StuartM almost 11 years
    Is there anyway to use one feature for IOS7 and the other for IOS6? I.e. the ECsldiing which was working fine for IOS6 and then the new for IOS7, I imagine it would be too much work to try and imp something like that
  • auco
    auco almost 11 years
    Yes, in code you can check for the current version with the officially recommended version check: if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { }
  • Pratik
    Pratik over 10 years
    @auco I want to Implement same thing in my project so, As you said in above 2nd comment "you don't need any third party sliding view controllers anymore on iOS7" so is there any default control for sliding menu in iOS 7? please tell me.
  • auco
    auco over 10 years
    @Praktik: It depends on the effect you want to achieve. Maybe you can do it with a UIViewControllerInteractiveTransitioning. Haven't worked with it much though, maybe you should open a separate question for this.
  • Karun
    Karun over 9 years
    Thanks a lot. Great explanation.
  • Mahesh Cheliya
    Mahesh Cheliya over 8 years
    is there anyway to hide status bar in ios 9 ?
  • Jonas
    Jonas over 7 years
    get {} is not needed, you can return true at once!
  • Tuan Anh Vu
    Tuan Anh Vu about 7 years
    finally! this is exactly what i was looking for. thanks!