How to change the status bar background color and text color on iOS 7?
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
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.
1) set the UIViewControllerBasedStatusBarAppearance to YES in the plist
2) in viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];
3) add the following method:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
UPDATE:
also check developers-guide-to-the-ios-7-status-bar
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