Hello, I have made attempts, but unfortunately there were not fruitful.
The setup is as follows, note that I went overboard with the toolbar modifiers just to be sure.
import SwiftData
import SwiftUI
@main
struct NewNavigationApp: App {
var body: some Scene {
WindowGroup {
NavigationControllerView {
ContentView()
}
}
}
}
@Observable
final class NavigationController: UINavigationController {
func pushView<Content: View>(_ view: @escaping () -> Content) {
let content = view()
.environment(self)
let controller = UIHostingController(rootView: content)
pushViewController(controller, animated: true)
}
}
struct NavigationControllerView<RootView: View>: UIViewControllerRepresentable {
@State private var navigationController = NavigationController()
private let rootView: () -> RootView
init(rootView: @escaping () -> RootView) {
self.rootView = rootView
}
func makeUIViewController(context: Context) -> NavigationController {
let view = rootView()
.environment(navigationController)
navigationController.viewControllers = [
UIHostingController(rootView: view)
]
return navigationController
}
func updateUIViewController(_ uiViewController: NavigationController, context: Context) { }
typealias UIViewControllerType = NavigationController
}
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
Color.blue
.ignoresSafeArea(.all)
.toolbarBackground(.purple, for: .navigationBar)
.toolbarColorScheme(.dark, for: .navigationBar)
}
.toolbarBackground(.purple, for: .navigationBar)
.toolbarColorScheme(.dark, for: .navigationBar)
}
}
#Preview {
NavigationControllerView {
ContentView()
.toolbarBackground(.purple, for: .navigationBar)
.toolbarColorScheme(.dark, for: .navigationBar)
}
.toolbarBackground(.purple, for: .navigationBar)
.toolbarColorScheme(.dark, for: .navigationBar)
}
However, the result was the same.
Next, I updated my info.plist though that did not change anything.
Afterwards, I attempted to create my own custom ViewController.
final class SwiftUIViewController<Content: View>: UIHostingController<Content> {
override var preferredStatusBarStyle: UIStatusBarStyle {
.darkContent
}
override init(rootView: Content) {
super.init(rootView: rootView)
view.backgroundColor = .purple
}
@MainActor @preconcurrency required dynamic init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and I have made the following modification inside struct NavigationControllerView<RootView: View>: UIViewControllerRepresentable
func makeUIViewController(context: Context) -> NavigationController {
let view = rootView()
.environment(navigationController)
navigationController.viewControllers = [
SwiftUIViewController(rootView: view)
]
return navigationController
}
so far, nothing has worked. I am aware this is an unorthodox way of using SwiftUI, I was playing around to see if I can use a SwiftUI App Lifecycle with Imperative navigation like navigation controllers, and adding the navigator to the view environment.