Always make the status bar text color white on some screens only

In the app, I set the brand color for the background.
So I want to change the color of the status bar to white, but on other pages I want to be able to change the color automatically.
How can I implement it in SwiftUI?

Replies

Since SwiftUI does not expose navigation styling directly, you'll need a workaround using UINavigationController.
For first declare a struct that ability you to change navigation styling:
Code Block swift
struct NavigationConfigurator: UIViewControllerRepresentable {
var configure: (UINavigationController) -> Void = { _ in }
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController {
UIViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
if let nc = uiViewController.navigationController {
self.configure(nc)
}
}
}


Then, you can call this like:
Code Block swift
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView {
Text("Hello world!")
}
.navigationBarTitle("Try it!", displayMode: .inline)
.background(NavigationConfigurator { nc in
nc.navigationBar.barTintColor = .blue
nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.white]
})
}
.navigationViewStyle(StackNavigationViewStyle())
}
}


Reference: https://stackoverflow.com/a/58427754/9503097