How to change Status Bar text color in iOS
Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the .plist file.In the
viewDidLoad
do a[self setNeedsStatusBarAppearanceUpdate];
Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Note: This does not work for controllers inside UINavigationController
, please see Tyson's comment below :)
Swift 3 - This will work controllers inside UINavigationController
. Add this code inside your controller.
// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 5 and SwiftUI
For SwiftUI create a new swift file called HostingController.swift
import Foundation
import UIKit
import SwiftUI
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Then change the following lines of code in the SceneDelegate.swift
window.rootViewController = UIHostingController(rootView: ContentView())
to
window.rootViewController = HostingController(rootView: ContentView())
Change status bar text colour from white iOS 7 / Xcode 5
I just fixed this issue in one of my apps. Implement this in your UIViewController:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Or choose another UIStatusBarStyle
value that you need.
How to change Status Bar text color in iOS
Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the .plist file.In the
viewDidLoad
do a[self setNeedsStatusBarAppearanceUpdate];
Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Note: This does not work for controllers inside UINavigationController
, please see Tyson's comment below :)
Swift 3 - This will work controllers inside UINavigationController
. Add this code inside your controller.
// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 5 and SwiftUI
For SwiftUI create a new swift file called HostingController.swift
import Foundation
import UIKit
import SwiftUI
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Then change the following lines of code in the SceneDelegate.swift
window.rootViewController = UIHostingController(rootView: ContentView())
to
window.rootViewController = HostingController(rootView: ContentView())
How can I change my status bar text color. Screenshot attached
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar. It does not allow to set a color (pink, as shown in your image) 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
}
Set value of .plist according to status bar style setup level.
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:
Changing the status bar text color in splash screen iOS 7
In the project plist file add the "Status Bar Style" property (key is UIStatusBarStyle
). Then ignore all the possible values listed in the drop down for this property and type UIStatusBarStyleLightContent
instead.
And you don't have to set UIViewControllerBasedStatusBarAppearance
to NO
in your plist, you can set the preferredStatusBarStyle
you want to your view controllers.
How to make status bar title color white in Swift?
You can simply do this in a swift file.
You can override this function in a viewcontroller class.
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
You don't need to set any value in info.plist.
Just above function will do.
But if you are going to change every view controller in white status colored, then you will need to set in AppDelegate.
Hope to make sense.
Is it possible to change the status bar text (foreground) color to an arbitrary color (i.e. not black or white)?
There is no documented way to change the text color to orange. However, it is definitively possible, because I just tried it out and it worked.
Disclaimer: this is all undocumented territorry... it will probably not be approved when you submit it to the app store. however, you may be lucky...
In iOS 7 you can do this:
/// sets the status bar text color. returns YES on success.
/// currently, this only
/// works in iOS 7. It uses undocumented, inofficial APIs.
BOOL setStatusBarColor(UIColor *color)
{
id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];
SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
// iOS 7+
[statusBar performSelector:setForegroundColor_sel withObject:color];
return YES;
} else {
return NO;
}
}
in iOS5 and iOS6 it may be possible too, but I haven't tried, because it is much more work. However I have found a method of interest (available in iOS 5 and iOS 6). Class UIStatusBarItemView
has an instance method called -textColorForStyle:
(which takes an integer and returns an object). You may be able to monkey-patch it to return any color you like.
How to set status bar's content color to white on iOS 7
- Set "View controller-based status bar appearance” to NO in your info.list file;
Insert
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
to
-application:didFinishLaunchingWithOptions:
of the AppDelegate.m.
Note: UIStatusBarStyleDefault
is the default value for the status bar style, it'll show black content instead. Both UIStatusBarStyleBlackTranslucent
& UIStatusBarStyleBlackOpaque
are deprecated in iOS 7.0.
UPDATE for iOS 9:
As @ZakariaDarwish mentioned, the method -setStatusBarStyle
is deprecated in iOS 9. (Note: The original question was asked for iOS 7 long time ago, and I don't support it now, the new solution below works for me under iOS 9, hence update here.)
So, the only way left (at least for now) is to implement -preferredStatusBarStyle
in your view controller (remember to set "View controller-based status bar appearance" back to YES).
You can invoke UIViewController's instance method -setNeedsStatusBarAppearanceUpdate
once value changed in -preferredStatusBarStyle
or -prefersStatusBarHidden
.
There're also two methods -childViewControllerForStatusBarStyle
& -childViewControllerForStatusBarHidden
to return the preferred style from child view controller as you want.
e.g., if you used below methods
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
to switch status bar style before, you can use code sample below
- (void)shouldChangeStatusBarStyleToLightContent:(BOOL)toLightContent
animated:(BOOL)animated
{
_shouldChangeStatusBarStyleToLightContent = toLightContent;
if (animated) {
[UIView animateWithDuration:.3f animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }];
} else {
[self setNeedsStatusBarAppearanceUpdate];
}
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return (_shouldChangeStatusBarStyleToLightContent ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault);
}
for this updated solution now.
Related Topics
Document or Cache Path Changes on Every Launch in iOS 8
iOS 11 Search Bar Jumping to Top of Screen
Open an External Link in Safari (Cordova)
Makekeywindow VS Makekeyandvisible
Google 400 Error: Invalid Request Custom Scheme Uris Are Not Allowed for 'Web' Client Type
What Does $0 Represent in Closures in Swift
Programmatically Detect Dark Mode in Swiftui to Display Appropriate Image
How to Register Undomanager in Swift
Game Engine Collison Bitmask... Why 0X01 etc
Getting System Uptime in iOS/Swift
Open Viewcontroller from Tab Like in Instagram Camera Page on iOS
How to Record a Video with Augmented Reality
Change Pin Image on Mkmapview in Swift
Nspredicate Filtered by Year Moth Day
How to Detect User Uninstalled iOS App? I Need Push Serious Notification. I Don't Wanna Lose It
Dequeuereusablecellwithidentifier:Forindexpath: VS Dequeuereusablecellwithidentifier: