Reputation: 895
Is there a way to change the status bar color if using the new app life cycle?
I know that for UIKit life cycle there is the workaround where you create your own HostingController
to overwrite the color.
But the new SwiftUI life cycle isn't using a UIHostingController
at all.
Upvotes: 2
Views: 490
Reputation: 54641
Here is a possible workaround:
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView() // or any other loading view
.onAppear(perform: UIApplication.shared.switchHostingController)
}
}
}
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
extension UIApplication {
func switchHostingController() {
windows.first?.rootViewController = HostingController(rootView: ContentView())
}
}
Two drawbacks I noticed:
.onAppear
- there might be a split second when you see the previous styleContentView
will be created twice at the beginningUpvotes: 3