mrd mrd - 6 days ago 6
iOS Question

How to set the navigation bar title from view controllers inside tabs?

I have a tab bar controller embedded in a navigation controller.
I want to set the title in the navigation from the view controllers inside the tabs.

If I use:

self.title = "ABC";


or

self.tabBarItem.title = "ABC";


the title of the tab at the bottom is set to "ABC", but not the title in the navigation bar at the top.

However, I only want to set the title in the navigation bar, not the tabs at the bottom.

How can I do that?

enter image description here

Answer

You have to pass the navigationItem up the chain.

The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController.

The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem

So to recap:

UINavigationController title = topViewController.navigationItem.title

UITabBarController tabTitle = selectedViewController.navigationItem.title

UIViewController title = navigationItem.title