Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 703
Janosch Hübner Avatar asked Jun 29 '20 14:06

Janosch Hübner


1 Answers

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 }
    }
}
like image 61
funkenstrahlen Avatar answered Nov 03 '22 01:11

funkenstrahlen