SwiftUI: How to change the status bar text color in SwiftUI?

For such purpose, we will need the UIHostingController. UIHostingController is a UIKit view controller that manages a SwiftUI view hierarchy.

First, we will need to create a new UIHostingController. And will specify the desired status bar content color.


import SwiftUI

class MainHost: UIHostingController<MySwiftUIContentView> {
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
}

struct MySwiftUIContentView: View {
    var body: some View {
        Text("My Swift UI Content View")
    }
}

Then we have to update our “scenewillConnectTo” function is SceneDelegate.swift file. At creation time, specify the SwiftUI view you want to use as the root view for this view controller.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       
       if let windowScene = scene as? UIWindowScene {
           let window = UIWindow(windowScene: windowScene)
           //This line
           window.rootViewController = MainHost(rootView: MySwiftUIContentView())
           self.window = window
           window.makeKeyAndVisible()
       }
   }