When you need to change the text color of status bar to white in order to enhance visibility you can proceed as shown below
- Set the
toUIViewControllerBasedStatusBarAppearance
YES
in the .plist file. - In the
viewDidLoad
do a[self setNeedsStatusBarAppearanceUpdate];
- Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
In order to cater for ViewControllers embedded in a NavigationController use this insted
// Preferred status bar style lightContent for dark background. override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
SwiftUI
For swift UI add a custom hosting file
//HostingController.swift //Created by Amarendra import Foundation import UIKit import SwiftUI class HostingController: UIHostingController<ContentView> { override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } }
Now you need to update the Scenedelegate.swift
to use your own HostingController.swift
// replace this line window.rootViewController = UIHostingController(rootView: ContentView()) window.rootViewController = HostingController(rootView: ContentView())
Hope this helps !!!