balbi
balbi

Reputation: 77

Navigation Controller doesn't show the title while working with TabBar Controller(Programmatically)

I'm using a navigationController as the window.rootViewController. And as the rootViewController of navigationController, I'm using a tabBarController. But when I run the app, the title of the navigationBar is not showing up (My Tabbar Controller shows up and I'm being able to see my viewController. No problem about that). I've tried to name the navigationBar in every viewController of the tabBar with using both of the code below in the viewDidload, but still nothing changed.

 navigationController?.title = "title"
 navigationController?.navigationBar.topItem?.title = "title"

I've set the navigationController with tabbar in the SceneDelegete as below:

let countriesTabbarController = TabBarControllerBuilder.build()
    
    let navigationController = UINavigationController(rootViewController: countriesTabbarController)
    
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

How can I show my navigationController title for every viewController of the tabBar?

Upvotes: 2

Views: 1444

Answers (1)

Julian Vogels
Julian Vogels

Reputation: 698

Each view controller has a property called navigationItem of type UINavigationItem. If you set its title, it will show up in the navigation bar if one exists.

For example in viewDidLoad, add this:

self.navigationItem.title = "title"

In other words: The title is set on the view controller contained in a navigation controller stack, not the navigation controller itself. Also not on the navigation bar, which probably also has a good reason I can't recall at the moment.

https://developer.apple.com/documentation/uikit/uinavigationitem

Upvotes: 3

Related Questions