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 }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With