iOS storyboard - Disable status bar on storyboard once
Asked Answered
M

2

12

Is there a way in Xcode when using the Storyboard in the Interface Builder to disable the status bar completely, so that when I create a new View Controller I don't keep having to turn "status bar" from inferred to none.

Mausoleum answered 23/1, 2012 at 17:26 Comment(0)
S
12

Have you already checked if setting UIStatusBarHidden to YES in your Info.plist works? See the documentation: http://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW15

This will hide the status bar on application start.

Scatter answered 23/1, 2012 at 17:41 Comment(2)
This only works when UIViewControllerBasedStatusBarAppearance is NOEasiness
I think the question could have been clearer. What I think the questioner wants to ask, and I want to too, is how to hide the status bar in the storyboard, so we dont see it in the storyboard too, not just when the app runs. For that case, this does not work in Xcode 9.3, with or without the additional setting given by @AlexanderFarberSubalternate
S
24

Here's a full iOS 7 compliant solution (not iOS 6 compatible though, as too many things have changed)

from the Apple doc ‘ : Information Property List Key Reference’ :

UIViewControllerBasedStatusBarAppearance (Boolean - iOS) specifies whether the status bar appearance is based on the style preferred by the view controller that is currently under the status bar. When this key is not present or its value is set to YES, the view controller determines the status bar style. When the key is set to NO, view controllers (or the app) must each set the status bar style explicitly using the UIApplication object. This key is supported in iOS 7.0 and later.

So by default this key is not present (= same behaviour as if you add the key, but set it to YES) : in this case each view controller determines the status bar behaviour, by overriding some methods - see below.

1) Globaly show/hide the status bar all of your app's viewControllers

so first the full answer to your question, to make a single, global show/hide setting for all of your apps viewControllers

step 1 : in your frameWork-Info.plst’ file, add the key ‘View controller-based status bar appearance’ and set it to ‘NO’. Setting this key to 'NO' will make iOS 7 not call the methods 'prefersStatusBarHidden' and 'preferredStatusBarUpdateAnimation', see further on.

step 2 : in project settings -> general -> deployment info, select 'show/hide' during application launch

that's it : now you can globally show or hide the status bar, compliant with iOS 7

2) Let all of your viewControllers have individual control over the status bar (= iOS 7 default)

step 1 : don't add the key, or add it, but set it to YES, to get the iOS 7 default behaviour

step 2 : in each viewController you can now determine if you show/hide the statusbar by overriding :

  • (BOOL)prefersStatusBarHidden
  • (UIStatusBarAnimation)preferredStatusBarUpdateAnimation

so you need to override these 2 methods to control the statusbar behaviour, and return the appropriate value for your application (see doc ref for all possible values, it's clearly explained).

to control the behaviour during app launch (very first controller that is shown), set show/hide under your project settings -> general -> deployment info

Note that iOS 7 only supports a single appearance style (UIStatusBarStyleLightContent), all others are deprecated !

You can thus easily toggle the bar on/off with a button for example - as flexible as possible !

for example, add a button to your viewController, and a bool @property 'isStatusBarHidden' Use the following code to toggle the bar on/off with animation.

- (BOOL)prefersStatusBarHidden
{
    return self.isStatusBarHidden;
}

- (IBAction)buttonToggleStatusBar:(UIButton *)sender
{
    [UIView animateWithDuration:1.0 animations:^{
        self.isStatusBarHidden = !self.isStatusBarHidden;
        [self setNeedsStatusBarAppearanceUpdate];
    }];

}

you can do the same for the preferred animation

advanced note : if you want to set a default value for the @property 'isStatusBarHidden', you need to do so in 'initWithCoder' (called when using Storyboards), instead of viewDidLoad. The reason is that 'prefersStatusBarHidden' and 'preferredStatusBarUpdateAnimation' are called before 'viewDidLoad'

Shavonda answered 15/12, 2013 at 12:51 Comment(1)
Re: "Note that iOS 7 only supports a single appearance style (UIStatusBarStyleLightContent), all others are deprecated !" Actually it supports two. UIStatusBarStyleDefault for light backgrounds and UIStatusBarStyleLightContent for dark backgrounds.Xyster
S
12

Have you already checked if setting UIStatusBarHidden to YES in your Info.plist works? See the documentation: http://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW15

This will hide the status bar on application start.

Scatter answered 23/1, 2012 at 17:41 Comment(2)
This only works when UIViewControllerBasedStatusBarAppearance is NOEasiness
I think the question could have been clearer. What I think the questioner wants to ask, and I want to too, is how to hide the status bar in the storyboard, so we dont see it in the storyboard too, not just when the app runs. For that case, this does not work in Xcode 9.3, with or without the additional setting given by @AlexanderFarberSubalternate

© 2022 - 2024 — McMap. All rights reserved.