10

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

1
  • 2
    I have the same issue and can reproduce this! Commented Jul 5, 2020 at 8:29

1 Answer 1

5

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 }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked! It's annoying the the "fix" is clean at least. FYI this still happens on iOS 14 but seems to be resolved on iOS 15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.