How to change Status Bar text color in iOS

How to change Status Bar text color in iOS

The status bar can have a dark and light appearance inside an app In this tutorial the appearance of the status bar will be changed

  • Open Xcode and create a new Single View App.
  • For product name, use IOSColorStatusBarTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.
  • Go to the Storyboard. Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content.

The style of the status bar can be changed to a status bar with white content. Go to the ViewController.swift file and add the following lines of code.

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

The preferredStatusBarStyle property is set to lightContent. Build and Run the project to see the content of the status bar changed to light.

Next, go back to the Storyboard, Select the View Controller and in the Editor menu Select Embed in Navigation Controller. Select the Navigation Bar and in the Attribute Inspector set the Bar Tint color to red. The Storyboard will look like this.

Build and Run the project, The content of the status bar is dark again, which is the default. The reason for this is, iOS asked for the style of the status bar of the navigation controller instead of the contained view controller.

To change the style of the navigation controller to lightinside the app, add the following viewDidAppear(_:) method

override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barStyle = .black
}

Build and Run the Project again, this time the content of the status bar changed to white.

There are Other options which we can try

#Option-1

Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file.

In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];

Add the following method:

- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}

 

#Option-2

you can opt out of the view-controller based status bar appearance:

Set View controller-based status bar appearance to NO in your Info.plist.

Call [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle on the UIViewController instead.

In AppDelegate add

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

to

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

}

#Option-3

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];

#Option-4:

Under the Info tab of the project target, Add Row:

UIViewControllerBasedStatusBarAppearance, set value NO.

#Option-5:

In the project AppDelegate.m:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}
#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];
}

Go to Project -> Target,

Then set Status Bar Style to Light. It makes status-bar white from the launch screen. Project Setting

Then set View controller-based status bar appearance equal to NO in Info.plist.

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())