You can find many (UIKit) solutions to set the text color of the status bar for a SwiftUI view. Unfortunately, in my experience, these solutions do not seem to work satisfactorily for TabViews at runtime.
Found Solutions:
SwiftUI: Set Status Bar Color For a Specific View
How can I change the status bar text color per view in SwiftUI?
Using these solutions, you will quickly find that the text color is not always set correctly when trying to switch between tabs.
Unfortunately, Apple currently doesn't seem to have a direct solution for SwiftUI to change the UIStatusBarStyle for each view like it was possible to do with UIKit.
I just can't find a stable way to change the status bar text color for each tab at runtime.
I read the other Solutions but did not try them. But the following works, and I also think it lets the other Solutions work.
UIViewControllerBasedStatusBarAppearance
to NO
If you don't mind having a deprecation warning in your Code this would also be an option:
TabView {
Text("1")
.tabItem { Text("1") }
.onAppear {
UIApplication.shared.setStatusBarStyle(.lightContent, animated: false)
}
Text("2")
.tabItem { Text("2") }
.onAppear {
UIApplication.shared.setStatusBarStyle(.darkContent, animated: true)
}
}
But setStatusBarStyle
is deprecated and therefore it might not be the best option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With