Note: If you have a tab bar controller with navigation controllers at the root of each view controller, setting the tab bar item on the view controllers won't affect the title if you're setting the navigationItem.title
. You'll need to set the tabBarItem
onto the navigation controller instead for it to be picked up from the tab bar controller.
None of the answers posted by others worked for me because my tab bar's view controllers all have navigation controllers at their root - this is a common hierarchy pattern for UITabBarController
. You have to set the navigation controller's tabBarItem
instead to get the title to show differently from the navigationItem
's title
You can create your tabBarItem
and associate them to your VC directly like so.
let tabBarVCOne = BooksListViewController()
tabBarVCOne.tabBarItem = UITabBarItem(title: "Books", image: nil, tag: 0)
tabBarViewControllers.append(tabBarVCOne)
...
Then you'll have something like this:
//Wrap each view controller in a navigation controller.
self.viewControllers = tabBarViewControllers.map(UINavigationController.init)
But that should be changed to the following in order to grab the already associated tabBarItem
from the view controller and set it onto the navigation controller automatically.
self.viewControllers = tabBarViewControllers.map({
let navigationController = UINavigationController(rootViewController: $0)
navigationController.tabBarItem = $0.tabBarItem
return navigationController
})
You will now be able to have a different title (set from your VC) separate from the title defined for your tabBarItem
.