Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xcode - iOS storyboard - Disable status bar on storyboard once

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...