Change the status bar background color color past iOS 7

Krunal picture Krunal · Jan 17, 2014 · Viewed 33.4k times · Source

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?

Answer

Teja Kumar Bethina picture Teja Kumar Bethina · Sep 25, 2014

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];