How to change the status bar background color and text color on iOS 7?
Solution 1
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
Solution 2
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.
Solution 3
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
Solution 4
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
Solution 5
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance
to NO
in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
- Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the.plist
file, if you need to set status bar style at UIViewController level only. In the viewDidLoad add function -
setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Rejeesh Rajan
Updated on July 08, 2022Comments
-
Rejeesh Rajan almost 2 years
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?
-
Muruganandham K over 10 yearsyou can change it to black or white
-
Johnykutty over 10 yearsthis is not about seting a text color
-
Dejell over 10 yearsGabriele, can you please provide code how to put 20px high view behind it?
-
Adam over 10 yearsThis has no effect (ios7, simulator). The "preferredStatusBarStyle" is never invoked.
-
Muruganandham K over 10 yearsare you using xib?. if YES change status bar value in simulated metrics property
-
Adam over 10 yearsAh, I found the issue. Apple's UINavigationController grabs the notification - i.e. your answer is only for when the view controller is the top controller, there are no containers (no tab-bar, no navbar, etc).
-
Shahid Iqbal over 10 yearsYour first two lines are correct.. But Last Line should be [navigationController.view addSubview:view]; It should be added inside UINavigationController's view not UINavigationBar's view as it will add view after 20 px of status bar not overlapping status bar.
-
ObjectiveTC about 10 yearsSpecial case when using Storyboard + NavigationController. Do #1 above. Next, create a subclass for UINavigationController (call it myNavController). In Storyboard, set the NavigationController's class to "myNavController". In myNavController.m, do #2 & #3 above. The method in #3 will now be called in your subclass (set a log or breakpoint to observe).
-
Joel Balmer about 10 yearsJust mentioning, apple docs recommend this if check instead: if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1){} else {} cheers!
-
Fattie about 10 yearsDejel, that's on Shahid's answer.
-
jpcguy89 almost 10 yearsThis is ridiculously difficult to figure out, thank you for providing the elusive answer.
-
learner over 9 yearsIn the info.plist I don't see how to
Set Status bar style to UIStatusBarStyleLightContent
. Or is it that I should not expect suggestions? Also is the value of type String? -
learner over 9 yearsAlso, inside which delegate method do I place the if block?
-
Shahid Iqbal over 9 years@learner Goto info.plist and then select any row. You will see a + sign. Click on Plus sign and from drop down, you should see
Status bar style
Option. Select it. And pasteUIStatusBarStyleLightContent
as its value. -
Shahid Iqbal over 9 years@learner I am talking about application AppDelegate Class. Every app has an AppDelegate class.
-
learner over 9 years@ShahidIqbal I understand that but which
method
? You simple show an if-block. In which method do I place the if-block? The delegate has a number of methods. Anyway, that was the question. But I figured it out. Thanks for replying. and +1 for the help. The method isdidFinishLaunchingWithOptions
-
lostintranslation over 9 yearsThis does not account for rotation
-
Benjamin Piette over 9 yearsYes this is even better to handle screen sizes and orientations. Also add something like this around this code: if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
-
user3344977 over 9 yearsAdding the subview was the only solution that worked for me when using a custom UINavigationBar in iOS 7 and 8. Thank you.
-
KlimczakM about 9 yearsIts better to use UIScreen width:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
-
Eneko Alonso about 9 yearsAlso consider the status bar being 40 pixels when the phone is on call or tethering modes.
-
Doug over 8 yearsA more succinct way to set the frame is using
UIApplication.sharedApplication().statusBarFrame
-
Michael over 8 yearsThis works, but I don't think it's really the best way to handle this style
-
Michael about 8 yearsUnlike the accepted solution, this also works when you change orientation. Thanks!
-
Robert J. Clegg almost 8 yearsWorks perfectly for me and the easiest solution.
-
Foriger almost 8 yearsIsn't that private API usage?
-
LordParsley almost 8 yearsThis worked well for me, but the status bar ought to be 20pt high: [[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 20)];
-
Sebyddd over 7 yearsI 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 :)
-
Warif Akhand Rishi over 7 years@Sebyddd That is bad news. I guess I was lucky all the time. Could you share us the error app store review team sent you. Did you need to deal with keyboard? What alternative approach did you take? Also I've added ur comment in the answer so people easily notice it. Thanks for sharing.
-
Sebyddd over 7 yearsThe message is pretty general and doesn't help much. I posted it bellow. In my case the keyboard was not an issue. I ended up slipping a view beneath the status bar at window level (something similar to what @shahid-iqbal did in his answer). "We found that your app uses one or more non-public APIs, which is not in compliance with the App Store Review Guidelines. The use of non-public APIs is not permissible because it can lead to a poor user experience should these APIs change."
-
Warif Akhand Rishi over 7 yearsI was hoping more form apple, like.. we don't want status bar bg changed.. or if u want to change bg do this.. :-( .. anyways.. thanks for the info @Sebyddd
-
JVS over 7 yearsIn Swift use this: let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIApplication.shared.statusBarFrame.height) let bar = UIView(frame: rect) bar.backgroundColor = UIColor.white navigationController?.view.addSubview(bar)
-
Mohammad Zaid Pathan over 7 yearsMake sure to call it after setting rootViewController.
-
user431791 over 7 yearswhy can't you directory change the background view color instead of creating a new view.. like
self.window?.rootViewController?.view.backgroundColor = UIColor.black
-
Fattie over 7 yearsOne of the best answers on the site, thanks @WarifAkhandRishi. I put in a long answer based on yours, which may further save people typing!
-
Fattie over 7 yearsDon't just use "20" ! You can get the value correctly, see my long answer below.
-
kelin over 7 yearsThis answer is super cool! I was searching a way to hide/modify Status Bar on simulator for iTunes Connect promo screens. With direct access to the bar I can customize it at any way.
-
LightNight about 7 yearsThanks for "UIViewControllerBasedStatusBarAppearance" key, helped me :)
-
Timeless about 7 yearsThere is an issue with this solution, when you double press the home button, this status status bar color will disappear.
-
Warif Akhand Rishi about 7 years@Timeless: Status bar is hidden from the iOS if you double press. So background color disappears.
-
halil_g about 7 yearsAdding a background image is an overkill if you just want to set a solid color, and infeasible especially if you want to be able to change the color on the fly
-
birdcage almost 7 yearsHow to make a single view controller's status bar transparent if we use this code?
-
dengApro over 6 yearsSo maybe it's better to change the UI backgroundColor by the view of the status bar position , nor the statusBar which is UIWindow controlled by the OS when you double press the home button. Like the answer(stackoverflow.com/questions/47250495/…), or other answer in this page.
-
abhimuralidharan over 6 yearsWill the app get rejected if we use these? Changing the statusBarView color like this is allowed?
-
Krunal over 6 years@abhi1992 I can't say whether apple will accept it or not because I implemented this solution in my enterprise application, which does not need to submit on App store. :)
-
Admin over 6 yearsIf I put this in a viewdidload of a viewcontroller in a tab-based app, it sets the color for every viewController, non only the one where I put the code (is this normal?)
-
Vladyslav Panchenko about 5 yearsThere is an issue with this solution, when you get a notification, status bar disappears too
-
NSDeveloper almost 5 yearsNot work on iOS 13. App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.
-
Ahmadreza almost 4 yearsWhat language is this?
-
Dan almost 4 years@Alfi Xamarin forms and in the background its C#
-
kelin almost 4 yearsYou will get double status bar on iOS 13.
-
Gonçalo Gaspar almost 4 yearsFor me this was the best answer since it mitigated a lot of other answer issues. The downside is the fixed frame.size.height = 64 which is wrong. One more recent way of getting the height would be -> .view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0.