Question or problem with Swift language programming:
My application has a dark background, but in iOS 7 the status bar became transparent. So I can’t see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?
How to solve the problem:
Solution 1:
-
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 { 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())
Solution 2:
Alternatively, you can opt out of the view-controller based status bar appearance:
- Set
View controller-based status bar appearance
toNO
in yourInfo.plist
. - Call
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle
on the UIViewController instead. (see Apple Developer Library)
Solution 3:
You can do this without writing any line of code!
Do the following to make the status bar text color white through the whole app
On you project plist file:
- Status bar style:
Transparent black style (alpha of 0.5)
- View controller-based status bar appearance:
NO
- Status bar is initially hidden:
NO
Solution 4:
Note: Most upvoted answer does not work for iOS 7 / 8
In Info.plist set ‘View controller-based status bar appearance’ as NO
In AppDelegate add
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ ...... ... }
This solution works for iOS 7 / 8
Solution 5:
For me, nothing happened with using all the things in the other answers (and from other sources/documentation). What did help was to set the Navigation Bar Style to “Black” in the XIB. This changed the text to white without any code at all.