1

I have some view controllers with a toolbar at top and look like this enter image description here

How I can fill the status bar background to match the toolbar background so its match the new iOS 7 style?

Anil
  • 2,539
  • 6
  • 33
  • 42
MohamMad Salah
  • 971
  • 2
  • 14
  • 31

1 Answers1

8

you need to add a subview in app delegate and change the color to your liking. here is a sample of the code that sits in applicationdidfinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    // Override point for customization after application launch.
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        UIView *addStatusBar = [[UIView alloc] init];
        addStatusBar.frame = CGRectMake(0, 0, 320, 20);
        //change this to match your navigation bar or view color or tool bar
        //You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
        addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
        [self.window.rootViewController.view addSubview:addStatusBar];
    }
    return YES;
}

take a look at the comments. you can use any type of color in addStatusBar.backGroundColor to match the color you need. note that the color with red here is just producing a Black background, change that to whatever you need. for dark grey replace that with the following code:

addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];

edit:

for changing the status bar color in individual views you simply plug in the following code ( which will access the view you want to change) in ViewDidLoad method right after [super viewDidLoad]; that should change the color of the status bar in just that view you place the code in.

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    UIView *addStatusBar = [[UIView alloc] init];
    addStatusBar.frame = CGRectMake(0, 0, 320, 20);
    addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
    [self.view addSubview:addStatusBar];

edit1:

in case of trying to add a subview on top of the nag bar in navigation controller, you have to twice the position of the subview, something like the following:

UIView *addStatusBar = [[UIView alloc] init];
    addStatusBar.frame = CGRectMake(0, -20, 320, 20);
    addStatusBar.backgroundColor = [UIColor colorWithRed:127.0/255. green:0.0/255. blue:127.0/255. alpha:1];
    [self.view addSubview:addStatusBar];
    [self.navigationController.navigationBar addSubview:addStatusBar];

and also you need to add your subview as yet the subview of the navigation controllers' navigation bar. i set the background color of the subview to purple but you can change that.

Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • This works for the rootViewController only. How about setting it at application level, for all view controllers? – user623396 Apr 28 '14 at 09:56
  • @user623396, for setting the color of status bar, (in this way, there are other ways to set it) i am just adding a subview to the top of the status bar with the coordinate and size. it works the same in view controller level as well but you have to access the view rather than rootviewcontroller. that maybe the case if you want to change the color of the status bar in different views to have different status bar color. i have modified the same code to work in view controller level in my post edit. let me know if that is what you looking for or need more help. – Adrian P Apr 28 '14 at 12:24
  • @XCode_Monkey thanks for your reply. The problem is, in fact, that adding the view like that will place it under the navigation bar, not at the top of the screen. – user623396 Apr 28 '14 at 12:44
  • So you are using navigation controller. Are trying to set a color of status bar different from the navigation bar? – Adrian P Apr 28 '14 at 13:00
  • Exactly. And the only way I did achieve this was by setting a background image on the navigation bar. First top 20 (or 40 on retina) pixels painted with desired color. Not the most flexible solution though. – user623396 Apr 28 '14 at 13:15