5
votes

I am updating my app to change the colour of my Navigation Bars from White to Blue. Therefore, I want to change the colour of the Status Bar from black to white. I've tried EVERYTHING from the Apple documentation, which helped me change the Status Bar Style for all my View Controllers in my Storyboard.

However, any nib I push to that is not in my Storyboard using presentViewController automatically changes the UIStatusBarStyle - here's my code for the push:

NSString *url = @"https://twitter.com/Example";
                NSString *title = @"Example";
SocialWebViewController *addController = [[[SocialWebViewController alloc] initWithURL:url title:title] initWithNibName:@"SocialWebView_iPhone" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion:nil];

I've already placed the UIStatusBar preferred style in my AppDelegate, which WORKS, BUT ONLY for the ViewControllers in my StoryBoard:

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

AND YES, I've already tried:

(1) [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

(2) [self setNeedsStatusBarAppearanceUpdate];

(3) Set the UIViewControllerBasedStatusBarAppearance to YES in the plist

NONE of this works for those Nibs not in my StoryBoard.

OK SO I FIGURED OUT WHAT YOU GUYS HAVE BEEN SAYING - SETTING View controller-based status bar appearance TO 'NO' HAS NOT BEEN WORKING FOR ME BECAUSE MY STATUS BAR IS INITIALLY HIDDEN DURING APPLICATION LAUNCH, WHICH HIDES IT ALL THE TIME WHEN I SET UIViewControllerBasedStatusBarAppearance to "NO"...How do I fix this?

2
Shouldn't UIViewControllerBasedStatusBarAppearance be NO? That way, it does not change automatically.erdekhayser
Its not bug, its feature.Toseef Khilji

2 Answers

7
votes

In your *-Info.plist file set the keys:

UIStatusBarStyle = UIStatusBarStyleLightContent
UIViewControllerBasedStatusBarAppearance = NO
UIStatusBarHidden = YES

And in your AppDelegate add this line to method application:didFinishLaunchingWithOptions:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
0
votes

If you want the style of the bar to be light in the entire application the easiest way is to set UIViewControllerBasedStatusBarAppearance to NO and then do

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Otherwise simply setting the preferredStatusBarStyle in the UIViewController should work without any other modifications.

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

My suspicion is that you are using a UINavigationController/UITabBarController etc to show the UIViewController. If that is the case then you will need to subclass UINavigationController/UITabBarController in order to add the correct status bar style you will need to subclass these controllers to add the preferredStatusBarStyle method to them. Simple implementation of such thing would be as follows

UINavigationControllerSubclass.h

#import <UIKit/UIKit.h>

@interface UINavigationControllerSubclass : UINavigationController

@end

UINavigationControllerSubclass.m

#import "UINavigationControllerSubclass.h"

@interface UINavigationControllerSubclass ()

@end

@implementation UINavigationControllerSubclass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end

Just use that instead of UINavigationController and the status bar should now be light.

You always have to think about stack of controllers when dealing with this kind of UI issue. Since the UINavigationController contains the UIViewController if you set the status bar property in just the UIViewController, the UINavigationController does not know about it and keeps its original property. That is why creating a subclass of UINavigationController and setting the status bar style there fixes everything. If you ever run into this kind of issue just try to remember that.