how to change uiviewcontroller title independent of tabbar item title

44,145

Solution 1

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"

Solution 2

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.

Solution 3

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"

Solution 4

For Swift use this,

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

Solution 5

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.

Share:
44,145

Related videos on Youtube

Atma
Author by

Atma

Updated on March 14, 2022

Comments

  • Atma
    Atma about 2 years

    I am setting my view controllers title like this in view did load:

    self.title = @"my title";
    

    prior to this I set the title in story boards for the view controller and navigation controller it is embedded in. I set it to: "Title";

    When I click on the tab that holds the view controller the title of tab bar item and uiviewcontroller change to: my title

    I would like for the view controller to change but the tab bar item to stay with the title: Title

    How can I accomplish this?

  • sam_smith
    sam_smith over 8 years
    Make sure, if you are still using self.title to set your uitabbar title, to set self.navigation setTitle after otherwise it won't work
  • Sushobhit
    Sushobhit over 6 years
    in xcode 9 and ios 11 in swift 4 it show error that unambiguous use of title
  • Sushobhit
    Sushobhit over 6 years
    in xcode 9 and ios 11 in swift 4 (self.navigation.title) showing error that unambiguous use of title
  • imike
    imike over 5 years
    What is topItem?
  • JohnnyC
    JohnnyC almost 5 years
    This was the only way I could set my top bar title in my configuration, not sure why "self.navigationItem.title" didn't work for me.
  • jangelsb
    jangelsb over 4 years
    Working for me in Xcode 10.2 using Swift 4.2, thanks a ton!