Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the status bar background color color past iOS 7

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?

like image 690
Krunal Avatar asked Jan 17 '14 07:01

Krunal


People also ask

How can I change status bar background color?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.

How do I change the color of my status bar in IOS 13?

To change the StatusBar background colour, i have created a base class of UIViewController so that i can inherit same code in all the view controllers. Added "statusBarColorChange()" to UIViewController extension. Created Base Class and inherited in other classes. Show activity on this post.

How do I change the color of my activity bar?

Just go to res/values/styles.edit the xml file to change the color of action bar.


2 Answers

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];
like image 151
Teja Kumar Bethina Avatar answered Sep 22 '22 18:09

Teja Kumar Bethina


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".

like image 23
Sahil Mahajan Avatar answered Sep 22 '22 18:09

Sahil Mahajan