UITabBar containing SwiftUI View

Aus*_*n E 3 uitabbarcontroller swift swiftui uihostingcontroller

I have the below series of controllers and views. However, when I use the navigation link on the MoreView it changes the tabBarItem.title value. For instance it will say more, but when the privacy policy button is clicked and the user is navigated to policy view, the title in the tab bar changes to whatever is in .navigationBarTitle() or if non is provided, an empty string! How do I avoid this? I want the tab bar title to be static

UITabBar -> UINavigationController -> MoreViewController(UIHostingController) -> MoreView(SwiftUI)

更多查看

List {
            
            Section(
                header: Text("ABOUT"),
                footer: Text(aboutFooter)
                        .font(.caption)
                ) {
                    NavigationLink(destination: WebView(
                        request: URLRequest(url: URL(string: "https://www.websitepolicies.com/policies/view/ng0sNvAJ")!)
                        )//.navigationBarTitle(Text("Privacy Policy"))
                    ) {
                        Text("Privacy Policy")
                    }
                    Text("Attribution")
            }
        }
        .listStyle(GroupedListStyle())
        .environment(\.horizontalSizeClass, .regular)
Run Code Online (Sandbox Code Playgroud)

fun*_*len 5

这将是 iOS 中的一个错误。请向 Apple 提交错误报告。

我刚刚发现了解决此问题的方法

创建一个自定义子类UINavigationController并将其用作包含您的MoreViewController.

class WorkaroundUINavigationController: UINavigationController {
    override var title: String? {
       get { tabBarItem.title }
       set { navigationItem.title = newValue }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我使用自定义 MoreViewController 时也遇到同样的情况。无论我做什么,这个解决方案似乎都会导致 getter 和 setter 之间出现循环。 (3认同)