Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change uiviewcontroller title independent of tabbar item title

It sounds like you want the title in the navigation bar to change but not the one in the tabbar. This should do that.

[self.navigationItem setTitle:@"my title"];

Swift:

self.navigationItem.title = "My Title"

So for those who still don't get it (like me)

self.navigationItem.title = @"my title"; sets navigation bar title.

self.tabBarItem.title = @"my title"; sets tab bar title.

self.title = @"my title"; sets both of these.


Swift

Set top bar title

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

Set tab item title

self.tabBarController?.tabBar.items?[0].title = "tab title"

Set both titles

self.title = "both titles"

For Swift use this,

self.navigationItem.title = "Navigation bar title" 
self.title = "Tab bar title"

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.


Swift 4.2

Here you go, I created an extension for UIViewController:

import UIKit

extension UIViewController {

/// Setting the navigation title and tab bar title
///
/// - Parameters:
///   - navigationTitle: Navigation title
///   - tabBarTitle: TabBar title
func setTitles(navigationTitle: String, tabBarTitle: String) {
    // Order is important here!
    title = tabBarTitle
    navigationItem.title = navigationTitle
 }

}

And then from your controller:

override func viewDidLoad() {
    super.viewDidLoad()
    setTitles(navigationTitle: "Login", tabBarTitle: "Home")
}