15
votes

I want to change background color of status bar on iOS 7, and I'm using this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [UIApplication sharedApplication].statusBarHidden = NO;

        self.window.clipsToBounds = YES;
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
        self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
        self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);

        ...
        ...


}

When I write this it shows the status bar with a black background; I want it to have a red background.

How can change the color of the status bar to have a red background instead of black?

8
You add subview of red color. read this once developer.apple.com/library/ios/documentation/userexperience/…Ayaz
There is no way to change color of status Bar ? other then adding subview.Krunal

8 Answers

27
votes

While handling the background color of status bar in iOS 7, there are 2 cases

Case 1: View with Navigation Bar

In this case use the following code in your viewDidLoad method

  UIApplication *app = [UIApplication sharedApplication];
  CGFloat statusBarHeight = app.statusBarFrame.size.height;

  UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
  statusBarView.backgroundColor = [UIColor yellowColor];
  [self.navigationController.navigationBar addSubview:statusBarView];

Case 2: View without Navigation Bar

In this case use the following code in your viewDidLoad method

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];
15
votes

In iOS 7 and later, the status bar is transparent. Set the backgroundColor of your view to the color you want for the status bar.

Or, you can add a 20px-high subview with red color at the top of your view.

See the Apple Transition Guide for more.

Also, make sure that your preferredStatusBarStyle is UIStatusBarStyleLightContent. and in your Info.plist set "View controller-based status bar appearance" to "NO".

8
votes

Objective c

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [[UIApplication sharedApplication] setStatusBarHidden:false];
    UIView *statusBar=[[UIApplication sharedApplication] valueForKey:@"statusBar"];
    statusBar.backgroundColor = [UIColor redColor];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];



    return YES;
}

This is working for me. I hope this will help you too.

5
votes

for what @Teja Kumar Bethina provided is helpful, but it's better to get the height of the statusBar from UIApplication singleton like below:

UIApplication *app = [UIApplication sharedApplication];

UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -app.statusBarFrame.size.height, self.view.bounds.size.width, app.statusBarFrame.size.height)];
    statusBarView.backgroundColor = [UIColor blackColor];
3
votes

in AppDelegate use

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

and in project info.plist file

set flag NO to View controller-based status bar appearance in app.

3
votes

Call this function from ViewDidLoad passing with your status bar color.

func setStatusBarBackgroundColor(color: UIColor)  
{
    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView 
    else 
    {
    return
    }
    statusBar.backgroundColor = color
}

Hope It'll help.

1
votes

Here's a swift solution that uses auto layout, If your application is in portrait or landscape the bar will be the correct width. It's added to the navigation bar view as a subview hence it's offset by -20. You could add it to your navigationbar's parent view, and then the offset is not required. This example will also set the colour to be the same as the navigation bar.

Hope that helps :)

    // Add colored opaque view behind the status bar view
    let statusBarView = UIView()
    statusBarView.backgroundColor = navigationBar.backgroundColor
    statusBarView.translatesAutoresizingMaskIntoConstraints = false
    navigationBar.addSubview(statusBarView)

    let views = ["statusBarView": statusBarView]
    let hConstraint = "H:|-0-[statusBarView]-0-|"
    let vConstraint = "V:|-(-20)-[statusBarView(20)]"

    var allConstraints = NSLayoutConstraint.constraintsWithVisualFormat(hConstraint,
            options: [], metrics: nil, views: views)

    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat(vConstraint,
            options: [], metrics: nil, views: views)

    NSLayoutConstraint.activateConstraints(allConstraints)
-4
votes

if your application is navigation based then you can set status bar background color red like this:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

may it will help.