简体   繁体   English

UIKit TabBar 与 SwiftUI 查看

[英]UIKit TabBar with SwiftUI View

I am updating my existing UIKit to use SwiftUI in some parts where I feel comfortable about being able to replace all of it with SwiftUI.我正在更新我现有的 UIKit 以在某些部分使用 SwiftUI,我觉得可以用 SwiftUI 替换所有这些部分。 It is a tab application, so there are 3 tabs and one of it are Settings, which I am replacing with SwiftUI.它是一个选项卡应用程序,因此有 3 个选项卡,其中一个是设置,我将其替换为 SwiftUI。 My tab bar is configured to only show images and no titles (the issue I need help with would also occur with labels shown).我的标签栏被配置为只显示图像而不显示标题(我需要帮助的问题也会出现在显示的标签上)。 Now in UIKit I do this:现在在 UIKit 我这样做:

private let settingsViewController: UIViewController = UIHostingController<SettingsView>(SettingsView())

and later on before showing it:稍后在展示之前:

settingsViewController.navigationItem.title = "Settings" . settingsViewController.navigationItem.title = "Settings"

However from then on SwiftUI takes on with its view modifier: .navigationBarTitle("Text") , which I use for nested view controllers (pushed via NavigationLink ), as UIKit cannot access those views to set a title.但是从那时起 SwiftUI 开始使用它的视图修饰符: .navigationBarTitle("Text") ,我将其用于嵌套视图控制器(通过NavigationLink推送),因为 UIKit 无法访问这些视图来设置标题。 The issue is however, that whenever .navigationBarTitle is used (same with setting the view's title property in UIKit, it sets the current tab bar items title. For UIKit there is always a way to set the navigationItem.title instead of title .然而,问题是,每当使用.navigationBarTitle时(与在 UIKit 中设置视图的title属性相同,它会设置当前选项卡栏项目的标题。对于 UIKit ,总有一种方法可以设置navigationItem.title而不是title

Is there any way to set the navigation bar title but not the tab bar title itself in SwiftUI?有没有办法在 SwiftUI 中设置导航栏标题而不是标签栏标题本身?

Thanks谢谢

This is to be a bug in iOS.这是 iOS 中的一个错误。 Please file a bug report to Apple.请向 Apple 提交错误报告。

I just discovered a workaround for this issue :我刚刚发现了解决此问题的方法

Create a custom subclass of UINavigationController and use it as the navigation controller containing your settingsViewController .创建UINavigationController的自定义子类,并将其用作包含您的settingsViewController的导航 controller 。

class WorkaroundUINavigationController: UINavigationController {
    override var title: String? {
       get { tabBarItem.title }
       set { navigationItem.title = newValue }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM