navigationTitle not working with UINavigationController inside a TabView in iOS 16

Setting a navigationTitle is not working anymore on iOS 16 when having UINavigationController inside a TabView. Run the code with iOS 14/15, no issue there. If Tabview is commented, navigation title appears for iOS 16 too. It seems the problem is caused somehow by the TabView. I know I can send the title as a parameter but I would prefer not to, also, for the moment, switching to NavigationVies is not an option.

import SwiftUI

@main
struct CustomUIKitNavigationApp: App {
    var body: some Scene {
        WindowGroup {
            TabView {
                NavigationViewControllerRepresentable {
                    VStack {
                        Text("why navigation title is not working anymore on iOS 16 when in TabView?")
                            .navigationTitle("navigation is not appearing")
                    }
                }
            }
        }
    }
}

struct NavigationViewControllerRepresentable<Content: View>: UIViewControllerRepresentable {
    let nav = UINavigationController()
    
    init(@ViewBuilder content: @escaping () -> Content) {
        let vc = HostingController(content: AnyView(content()))
        nav.addChild(vc)
    }

    func makeUIViewController(context: Context) -> UINavigationController {
        return nav
    }

    func updateUIViewController(_ pageViewController: UINavigationController, context: Context) {}
}

class HostingController: UIHostingController<AnyView> {
    
    init(content: AnyView) {
        super.init(rootView: AnyView(content))
    }

    @objc required dynamic init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) not implemented")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
}


Post not yet marked as solved Up vote post of cstefaniga Down vote post of cstefaniga
568 views