I wanted to experiment a little using UINavigationController with SwiftUI App Lifecycle
I created a Custom Navigation Controller named JDNavigationController. It does not do much other than having the @Observable macro
public struct NavigationControllerStack<Content: View>: UIViewControllerRepresentable {
private let initialView: () -> Content
@State private var controller = JDNavigationController()
public init(content: @escaping () -> Content) {
self.initialView = content
}
public func makeUIViewController(context: Context) -> JDNavigationController {
let viewController = self.initialView()
.environment(self.controller)
.viewController
self.controller.viewControllers = [
viewController
]
return controller
}
public func updateUIViewController(_ uiViewController: JDNavigationController, context: Context) {
}
public typealias UIViewControllerType = JDNavigationController
}
when I check, the functionality of the navigation controller works as expected, however, I can't for the life of me to change the background color of the status bar.
#Preview {
NavigationControllerStack {
ZStack {
Color.purple
.ignoresSafeArea(.all)
VStack {
Text("somethign")
}
}
}
}
I have tried
-
Using the appearance functions
-
Creating a Custom View Controller, and in that view controller I tried using a Hosting Controller by adding the hosting controller as a child and
setting hostingController.view.insetsLayoutMarginsFromSafeArea = false
- using the toolbar modifiers from SwiftUI View
none seems to work.
I opened the View Debugger it looks like the white portion comes from the UIHosting Controller but I am not sure what I can do to fix it
@lukdor TN3105: Customizing the UIKit status bar style covers the UIKit Component side of this.
In SwiftUI, you could change the background color using toolbarColorScheme(_:for:)
and toolbarBackground(_:for:)