How to change Status Bar text color in iOS ?

Your application has a dark background and status bar became transparent so you can’t see anything their you can show only the green battery indicator in the corner. Here’s how you can change the status bar text color by customizing the navigation bar.

change Status Bar text color in iOS

Solution – 1

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file. 
  2. In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];
  3. Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle
{ 
    return UIStatusBarStyleLightContent; 
}

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

For anyone using a UINavigationController:

The UINavigationController does not forward on preferredStatusBarStyle calls to its child view controllers. Instead it manages its own state – as it should, it is drawing at the top of the screen where the status bar lives and so should be responsible for it. Therefor implementing preferredStatusBarStyle in your VCs within a nav controller will do nothing – they will never be called.

The trick is what the UINavigationController uses to decide what to return for UIStatusBarStyleDefault or UIStatusBarStyleLightContent. It bases this on its UINavigationBar.barStyle. The default (UIBarStyleDefault) results in the dark foreground UIStatusBarStyleDefault status bar. And UIBarStyleBlack will give a UIStatusBarStyleLightContent status bar.

TL;DR:

If you want UIStatusBarStyleLightContent on a UINavigationController use:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

Solution – 2

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 – 3

Here’s how you can change the status bar text color by customizing the navigation bar:

  1. Set the preferredStatusBarStyle property in your view controller:

In your view controller’s implementation file (e.g., ViewController.m or ViewController.swift), override the preferredStatusBarStyle property to specify the status bar style.

Swift:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent // .default for dark text, .lightContent for light text
}

Objective-C:

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent; // UIStatusBarStyleDefault for dark text, UIStatusBarStyleLightContent for light text
}
  1. Customize the navigation bar appearance:

In the viewDidLoad method of your view controller, customize the navigation bar appearance to control the status bar text color indirectly.

Swift:

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the navigation bar background color
    navigationController?.navigationBar.barTintColor = UIColor.red // Replace with your desired color

    // Set the navigation bar text color
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] // Replace with your desired color
}

Objective-C:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the navigation bar background color
    self.navigationController.navigationBar.barTintColor = [UIColor redColor]; // Replace with your desired color

    // Set the navigation bar text color
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; // Replace with your desired color
}

By setting the navigation bar appearance, you can control the status bar text color. The preferredStatusBarStyle method will be called whenever the view controller is presented, and the status bar text color will be updated accordingly.

if you issue with changing navigation bar – background color, text color click here for Other Solution.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *