在 iOS 16 中,navigationTitle 无法与 TabView 内的 UINavigationController 配合使用

Cla*_*diu 5 navigation uikit ios tabview swiftui

当 TabView 中包含 UINavigationController 时,在 iOS 16 上设置 navigationTitle 不再有效。使用 iOS 14/15 运行代码,没有问题。如果 Tabview 被注释,iOS 16 也会出现导航标题。看来问题是由 TabView 引起的。我知道我可以将标题作为参数发送,但我也不想这样做,目前,切换到 NavigationVies 也不是一个选择。

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*ump 0

添加.tabViewStyle(.page)应该使导航栏标题出现。但我也无法让大显示模式标题出现,而是始终将其内联。

TabView {
  CustomUINavigationControllerView()
}.tabViewStyle(.page)
Run Code Online (Sandbox Code Playgroud)