简体   繁体   中英

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. It is a tab application, so there are 3 tabs and one of it are Settings, which I am replacing with 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:

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

and later on before showing it:

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. 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 .

Is there any way to set the navigation bar title but not the tab bar title itself in SwiftUI?

Thanks

This is to be a bug in iOS. Please file a bug report to 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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