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?
Set the UIViewControllerBasedStatusBarAppearance
to YES
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())
Alternatively, you can opt out of the view-controller based status bar appearance:
View controller-based status bar appearance
to NO
in your Info.plist
.[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle
on the UIViewController instead. (see Apple Developer Library )
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:
Transparent black style (alpha of 0.5)
NO
NO
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
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.
None of that worked for me, so here is a working solution...
In Info.plist
, add a row:
UIViewControllerBasedStatusBarAppearance
, and set the value NO
.
Then in AppDelegate in didFinishLaunchingWithOptions
, add these rows:
[application setStatusBarHidden:NO];
[application setStatusBarStyle:UIStatusBarStyleLightContent];
You dont need to do any code for this
You need to add "View controller-based status bar appearance" key in info.plist as follows:
& set its value type to Boolean & value to NO. Then click on project settings,then click on General Tab & under Deployment Info set the preferred status bar style to .Light as follows:
Thats it.
Just two steps as following:
Step 1:
Under the Info tab of the project target
, Add Row:
UIViewControllerBasedStatusBarAppearance
, set value NO
.
Step 2:
In the project AppDelegate.m
:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}
This works in Golden Master iOS 7 and Xcode 5 GM seed and iOS7 SDK released on September 18th, 2013 (at least with navigation controller hidden):
Set the UIViewControllerBasedStatusBarAppearance
to NO
in the Info.plist
.
In ViewDidLoad
method or anywhere, where do you want to change status bar style: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
In case your UIViewController is inside a UINavigationController you will have to set the BarStyle:
-[UINavigationBar setBarStyle:UIBarStyleBlack]
Original Answer is here
If you have an embedded navigation controller created via Interface Builder , be sure to set the following in a class that manages your navigation controller:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
That should be all you need.
In AppDelegate.m, add the following.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
And in the Plist file, set 'View controller-based status bar appearance' to NO.
In Swift 3 is very easy just with 2 steps. Go to your info.plist and change the key
View controller-based status bar appearance
to "NO". Then in the Appdelegate just add this line in didfinishlaunchingwithoptions method
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.statusBarStyle = .lightContent
return true
}
this has been deprecated in iOS9 now you should do override this property in the rootviewcontroller
doing this has been deprecated in iOS 9 should do this on the rootviewcontroller
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Well, this is really working like a piece of cake for me.
Go to your app's info.plist
.
View controller-based status bar appearance
to NO
Status bar style
to UIStatusBarStyleLightContent
Then go to your app's delegate and paste in 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,320, 20)];
view.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
[self.window.rootViewController.view addSubview:view];
}
Bingo. It's working for me.
只需在 Appdelegate 中
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
iOS 7 allows individual view controllers to determine the appearance of the status bar, as described by the Apple developer documentation:
iOS 7 gives view controllers the ability to adjust the style of the status bar while the app is running. A good way to change the status bar style dynamically is to implement
preferredStatusBarStyle
and—within an animation block—update the status bar appearance and callsetNeedsStatusBarAppearanceUpdate
.
Setting the status bar appearance globally is a two-step process.
First, you need to tell iOS that you don't want to set the status bar appearance on a view-by-view basis.
Then you need to take charge and actually set the new global status bar style.
To disable view-by-view status bar control, you'll need to set the View controller-based status bar appearance
property in Info.plist
.
Open the Project Navigator and select the project for your iOS app, then select the Info tab.
Hover over a row, then click the plus sign that appears to add a new property to your .plist
.
Enter View controller-based status bar appearance
in the Key field, then make sure the Type field is set to Boolean
. Finally, enter NO
in the Value field.
To set a global style for the status bar, add another property under the Info tab with a key of Status bar style
, a Type of String
and a Value of Opaque black style
.
Here's a blog post with a little more detail and some sample code:
http://codebleep.com/setting-the-status-bar-text-color-in-ios-7/
Answer updated for for Xcode GM Seed:
In Info.plist
put View controller-based status bar appearance
as NO
In the project, set:
In ViewDidLoad:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
不需要做一些额外的事情,只需在您的 viewController 中编写此代码并获得状态栏颜色为白色
- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}
I think all the answers do not really point the problem because all of them work in specific scenarios. But if you need to cover all the cases follow the points bellow:
Depending on where you need the status bar light style you should always have in mind these 3 points:
1)If you need the status bar at the launch screen or in other places, where you can't control it (not in view controllers, but rather some system controlled elements/moments like Launch Screen) You go to your project settings
2) if you have a controller inside a navigation controller You can change it in the interface builder as follows:
a) Select the navigation bar of your navigation controller
b) Then set the style of the navigation bar to "Black", because this means you'll have a "black" -> dark background under your status bar, so it will set the status bar to white
Or do it in code as follows
navigationController?.navigationBar.barStyle = UIBarStyle.Black
3) If you have the controller alone that needs to have it's own status bar style and it's not embedded in some container structure as a UINavigationController
Set the status bar style in code for the controller:
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:
UIViewControllerBasedStatusBarAppearance
to YES
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.
Here is some hacky trick to change/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:
This is documented in the iOS 7 UI Transition Guide , which you need an Apple developer ID to access directly. The relevant excerpt:
Because the status bar is transparent, the view behind it shows through. [...] Use a
UIStatusBarStyle
constant to specify whether the statusbar content should be dark or light:
UIStatusBarStyleDefault
displays dark content. [...]
UIStatusBarStyleLightContent
displays light content. Use when dark content is behind the status bar.
Also possibly of interest:
In iOS 7, you can control the style of the status bar from an individual vew controller and change it while the app runs. To opt in to this behavior, add the
UIViewControllerBasedStatusBarAppearance
key to an app'sInfo.plist
file and give it the valueYES
.
I'd definitely recommend having a look through the document, which, again, you can access with your Apple developer ID.
Simply calling
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
in the
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}
method of my AppDelegate
works great for me in iOS7.
I did some things different and it works for me.
With no changes in code, I did config my .plist file like this:
I hope it helps.
For each view controller I change the "status bar"'s Simulated Metrics property, in storyboard, from "inferred" to "Light Content"
如果您仍然想在 info.plist 中使用View controller-based status bar appearance
设置为 YES,这意味着您可以更改每个视图控制器的状态栏,请在 ViewDidLoad 中的状态栏中使用以下白色文本:
[[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent];
在 info.plist 中设置字段值 NO View controller-based status bar appearance并在目标> 常规设置中设置状态栏样式灯。
If you want the same result with Swift, you can use this code in your AppDelegate.swift file :
UINavigationBar.appearance().barStyle = .BlackTranslucent
And the text of your status bar will be white :-) !
In my case for Swift 5, I added these lines:
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barStyle = .black
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Xcode constantly seems to change this, so this is the latest.
To change the status bar to white:
UIViewControllerBasedStatusBarAppearance
and set value to No
(false).UIStatusBarStyle
and set value to UIStatusBarStyleLightContent
(ie, light content).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?
Just to summarize, edit your project Info.plist
and add:
View controller-based status bar appearance
: NO
Status bar style
: Opaque black style
or if you have raw key/value plist
UIViewControllerBasedStatusBarAppearance
: NO
UIStatusBarStyle
: Opaque black style
Let me give you a complete answer to your question. Changing the status bar text color is very easy but its a little confusing in iOS 7 specially for newbies.
If you are trying to change the color from black to white in Storyboard by selecting the view controller and going to Simulated Metrics on the right side, it won't work and i don't know why. It should work by changing like this but any how.
Secondly, you won't find UIViewControllerBasedStatusBarAppearance property in your plist but by default its not there. You have to add it by yourself by clicking on the + button and then set it to NO.
ios 7 status bar text color
Lastly, you have to go to your AppDelegate.m file and add the following in didFinishLaunchingWithOptions method, add the following line:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
This will change the color to white for all your view controllers. Hope this helps!
The key to making this work is that only the fullscreen view controller get's to dictate the style of the status bar.
If you are using a navigation controller and want to control the status bar on a per view controller basis, you'll want to subclass UINavigationController and implement preferredStatusBarStyle such that it returns the topViewController's preference.
Make sure you change the class reference in your storyboard scene fromUINavigationController to your subclass (eg MyNavigationController in the example below).
(The following works for me. If your app is TabBar based, you'll want to do something similar by subclassing the UITabBarController but I haven't tried that out).
@interface MyNavigationController : UINavigationController
@end
@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return self.topViewController.preferredStatusBarStyle;
}
@end
For Xcode 5.1:
Add " View controller-based status bar appearance " to NO in the .plist.
In AppDelegate, add:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
That's all!
Swift 3 - Xcode 8.
If you want status bar to initially hidden on Launch screen then try this,
Step 1: Add following to info.plist
.
View controller-based status bar appearance
value NO
Status bar is initially hidden
value YES
Step 2: Write this in didFinishLaunchingWithOptions
method.
UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
You can do this from info.plist:
1) "View controller-based status bar appearance" set to "NO"
2) "Status bar style" set to "UIStatusBarStyleLightContent"
done
In Info.plist set 'View controller-based status bar appearance' as NO
In AppDelegate add
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
change the status bar text color for all ViewControllers
swift 3
if View controller-based status bar appearance = YES in Info.plist
then use this extension for all NavigationController
extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
if there is no UINavigationController and only have UIViewController then use Below code:
extension UIViewController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
objective c
create category class
For UIViewController
In UIViewController+StatusBarStyle.h
@interface UIViewController (StatusBarStyle)
@end
In UIViewController+StatusBarStyle.m
#import "UIViewController+StatusBarStyle.h"
@implementation UIViewController (StatusBarStyle)
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
For UINavigationController
In UINavigationController+StatusBarStyle.h
@interface UINavigationController (StatusBarStyle)
@end
In UINavigationController+StatusBarStyle.m
#import "UINavigationController+StatusBarStyle.h"
@implementation UINavigationController (StatusBarStyle)
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
In iOS 8: add NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
to viewDidLoad
I make this in iOS 9 and Swift 2.0 if I use UINavigationController
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black
And I make this if I use modal segue
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
If your application needs to have UIStatusBarStyleLightContent
by default, but you still want to have the ability to use UIStatusBarStyleDefault
on some screens, you could choose to manage the status bar color on the controller level, but in this case you'll have to overwrite preferredStatusBarStyle
in every view controller (or implement it in a base view controller, from which all your other view controllers will inherit). Here's another way of solving this problem:
UIViewControllerBasedStatusBarAppearance
to NO
in the plistUIStatusBarStyle
to UIStatusBarStyleLightContent
All view controllers will use white text for the status bar. Now add this methods only in the view controllers that need the status bar with black text:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
The easiest way to do this from Xcode (without any coding) is:
View controller-based status bar appearance
to your Info.plist and set the value to NO
.Deployment Info
you'll find an option for Status Bar Style
. Set the value of this option to Light
. You'll have the White
status bar.
UINavigationController
is a subclass of UIViewController
(who knew 🙃)!
Therefore, when presenting view controllers embedded in navigation controllers, you're not really presenting the embedded view controllers; you're presenting the navigation controllers! UINavigationController
, as a subclass of UIViewController
, inherits preferredStatusBarStyle
and childForStatusBarStyle
, which you can set as desired.
Any of the following methods should work:
info.plist
, add the following property:
UIUserInterfaceStyle
(aka. "User Interface Style")Override preferredStatusBarStyle
within UINavigationController
preferredStatusBarStyle
( doc ) - The preferred status bar style for the view controller Subclass or extend UINavigationController
class MyNavigationController: UINavigationController { override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent } }
OR
extension UINavigationController { open override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent } }
Override childForStatusBarStyle
within UINavigationController
childForStatusBarStyle
( doc ) - Called when the system needs the view controller to use for determining status bar style "If your container view controller derives its status bar style from one of its child view controllers, [override this property] and return that child view controller. If you return nil or do not override this method, the status bar style for self is used. If the return value from this method changes, call the setNeedsStatusBarAppearanceUpdate() method."
Subclass or extend UINavigationController
class MyNavigationController: UINavigationController { override var childForStatusBarStyle: UIViewController? { topViewController } }
OR
extension UINavigationController { open override var childForStatusBarStyle: UIViewController? { topViewController } }
You can return any view controller you'd like above. I recommend one of the following:
topViewController
(of UINavigationController
) ( doc ) - The view controller at the top of the navigation stack visibleViewController
(of UINavigationController
) ( doc ) - The view controller associated with the currently visible view in the navigation interface (hint: this can include "a view controller that was presented modally on top of the navigation controller itself") Note: If you decide to subclass UINavigationController
, remember to apply that class to your nav controllers through the identity inspector in IB.
PS This works on iOS 13 😎
This does seem to be an issue with the current build of Xcode and iOS 7.
Some related content on Apple's Developer Forums is in a search for UIStatusBarStyleLightContent in "iOS 7 Beta Livability" on the Apple Developer Forums * (currently 32 posts).
I came across it trying to set it to the light version.
(This is just a follow up on Aaron's answer.)
Delete the View controller-based status bar appearance in .plist file (if u have create) and recreate it.
set Status Bar style to Opaque black style
In appDelegate add the following code under didFinishLaunching.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
This worked for me:
Set the UIViewControllerBasedStatusBarAppearance
to YES
in the plist
The rootViewController
needs the method implementation for
-(UIStatusBarStyle)preferredStatusBarStyle
Because my rootViewController
is managed by Cocoapods ( JASidePanelController
) I added this method through a category:
#import "JASidePanelController+StatusBarStyle.h"
@implementation JASidePanelController (StatusBarStyle)
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
Just Change in 1) Info.plist
View controller-based status bar appearance
-> NO
and write 2)
[[UIApplication
sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
On iOS 7, if you want to use UIViewControllerBasedStatusBarAppearance == YES, and your root view controller is UINavigationController, you should subclass it and overload childViewControllerForStatusBarStyle, for example, like this:
- (UIViewController*) childViewControllerForStatusBarStyle
{
return self.viewControllers.lastObject;
}
After this, preferredStatusBarStyle will be called on pushed view controllers.
You can use this for iOS 6 and 7:
#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif
[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
What I had to do for swift and navigation controller
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Please try this
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor blackColor];
}
If you want to set it to any color, use below code.
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:YourColorHere];
^^^^^^^^^^^^^
}
I know I am accessing private API, but I have used this in many projects and Apple have approved it.
Just while submitting the app, send this code to Apple in Comments section and inform that you are using this code to change the status bar color.
And yes , don't forget below too.
Here is a better solution extend Navigation controller and put in storyboard
class NVC: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.isHidden = true
self.navigationController?.navigationBar.isTranslucent = false
self.navigationBar.barTintColor = UIColor.white
setStatusBarColor(view : self.view)
}
func setStatusBarColor(view : UIView){
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor.black
view.addSubview(statusbarView)
statusbarView.translatesAutoresizingMaskIntoConstraints = false
statusbarView.heightAnchor
.constraint(equalToConstant: statusBarHeight).isActive = true
statusbarView.widthAnchor
.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusbarView.topAnchor
.constraint(equalTo: view.topAnchor).isActive = true
statusbarView.centerXAnchor
.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.black
}
}
}
status bar color will be black and text will be white
This solution works for apps using the new SwiftUI Lifecycle / iOS 14.0 :
I needed to change the status bar text color dynamically and couldn't access window.rootViewController
because SceneDelegate
doesn't exist for the SwiftUI Lifecycle.
I finally found this easy solution by Xavier Donnellon: https://github.com/xavierdonnellon/swiftui-statusbarstyle
Copy the StatusBarController.swift
file into your project and wrap your main view into a RootView
:
@main
struct ProjectApp: App {
var body: some Scene {
WindowGroup {
//wrap main view in RootView
RootView {
//Put the view you want your app to present here
ContentView()
//add necessary environment objects here
}
}
}
}
Then you can change the status bar text color by using the .statusBarStyle(.darkContent)
or .statusBarStyle(.lightContent)
view modifiers, or by calling eg UIApplication.setStatusBarStyle(.lightContent)
directly.
Don't forget to set "View controller-based status bar appearance" to "YES" in Info.plist.
In my case nothing helped. I was trying to change StatusBar
color at the ViewController2
, that was embded in the NavigationController
, which, in turn, was presented modally from ViewController1
. This way not worked:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
Nothing happened, until I found this solution: Add to ViewController1
this line -
navigationController.modalPresentationCapturesStatusBarAppearance = true
let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)
So if you have navigation scheme similar to ViewController1 presented ViewController2
, try modalPresentationCapturesStatusBarAppearance
property of the presented one
The default value of this property is false.
When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen.
The system ignores this property's value for a view controller presented fullscreen.
This answer is with the help of hackingwithswift website
for iOS (13, *)
Some Time we need different colour of status bar, for example for one ViewController we need the black status bar, and for the second ViewController we need the white status bar. Now what we have to do? We need to add this peace of code in ViewController
// MARK: - Variables
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// MARK: - View Life Cycle
override func viewDidAppear(_ animated: Bool) {
setNeedsStatusBarAppearanceUpdate()
}
This code will change the light or White colour of status bar in that particular ViewController. We can change it to .dark in preferredStatusBarStyle
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
Very Easy way To change the status bar color. Create the subclass of navigation Controller.
Write this code in view didload method. Effect this code in all view controller
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
[UIColor whiteColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.