Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the status bar background color and text color on iOS 7?

My current application runs on iOS 5 and 6.

The navigation bar is having an orange color and the status bar is having a black background color with white text color. However, when I run the same application on iOS 7, I observe the status bar looks transparent with the same orange background color as the navigation bar and the status bar text color is black.

Due to this I'm not able to differentiate between the status bar and the navigation bar.

How do I make the status bar to look the same as it was in iOS 5 and 6, that is with black background color and white text color? How can I do this programmatically?

like image 739
Rejeesh Rajan Avatar asked Sep 28 '13 04:09

Rejeesh Rajan


People also ask

How do I change the color of my status bar on my iPhone?

Go to the Storyboard. Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content.

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 can I change my status bar theme?

How do I change the status bar color which should be same as in navigation bar. As Niels suggests, there is a THEME ATTRIBUTE since v21 called android:statusBarColor . You can simply add it to values-v21/styles. xml .

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.


3 Answers

Warning: It does not work anymore with iOS 13 and Xcode 11.

========================================================================

I had to try look for other ways. Which does not involve addSubview on window. Because I am moving up the window when keyboard is presented.

Objective-C

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

Swift

func setStatusBarBackgroundColor(color: UIColor) {

    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }

    statusBar.backgroundColor = color
}

Swift 3

func setStatusBarBackgroundColor(color: UIColor) {

    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }

    statusBar.backgroundColor = color
}

Calling this form application:didFinishLaunchingWithOptions worked for me.

N.B. We have an app in the app store with this logic. So I guess it is okay with the app store policy.


Edit:

Use at your own risk. Form the commenter @Sebyddd

I had one app rejected cause of this, while another was accepted just fine. They do consider it private API usage, so you are subject to luck during the reviewing process :) – Sebyddd

like image 63
Warif Akhand Rishi Avatar answered Oct 17 '22 12:10

Warif Akhand Rishi


Goto your app info.plist

1) Set View controller-based status bar appearance to NO
2) Set Status bar style to UIStatusBarStyleLightContent

Then Goto your app delegate and paste the following code where you set your Windows's RootViewController.

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

Hope it helps.

like image 110
Shahid Iqbal Avatar answered Oct 17 '22 10:10

Shahid Iqbal


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

Source link http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html

like image 29
Teja Kumar Bethina Avatar answered Oct 17 '22 10:10

Teja Kumar Bethina