2

I have app with a tabBar and Navigation controller.

How can I change the tabBar title (visible in top application's window) when I touch it in tabbar?

For example i have these items in tabbar:

  • Pizza
  • Beer
  • Orange
  • Apple

After I click pizza I want to have pizza in the title app in the top menu.

How can I do this?

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
  • 1
    Possible duplicate of [How to set the title of a Navigation Bar programmatically?](https://stackoverflow.com/questions/6154237/how-to-set-the-title-of-a-navigation-bar-programmatically) – Gereon Mar 02 '18 at 13:31

1 Answers1

4

Depending on your implementation, one of the below methods should work for you.

self.navigationItem.title = "title"

or

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

If you are using a custom tabbar made with UIButtons and container view, then add this to the button action or if you are using a native UITabBarController, then set it's delegate to self and call this on the didSelectViewController delegate method of the UITabBarController.

.

EDIT

After seeing your code, you need to use this property :

self.tabBarController?.navigationItem.title = "Profile"

and call this in every view controller's viewWillAppear, example for ProfileViewController

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.navigationItem.title = "Profile"
    }

Also, make sure that in storyboard, you set the view controller's class to the respective code class like :

add class to view controller in storyboard

and remove the text from the custom navigation bar you used:

remove the text from navigation bar

Umar Farooque
  • 2,049
  • 21
  • 32
  • where to add it? To viewcontroller selected items from tabbar? – Karol Dupczyk Mar 02 '18 at 13:32
  • if you are using a custom tabbar made with UIButtons, then add this to the action listener or if you are using a native UITabBarController, then set it's delegate to self and call this on the didSelectViewController delegate method of the UITabBarController – Umar Farooque Mar 02 '18 at 13:35
  • This is my source code: https/we.tl/6PW31OJwmi . I have problem with animated tab bar. Could you take a look? – Karol Dupczyk Mar 02 '18 at 13:52
  • cool, please upvote as well ! and for using the button, use a similar logic to find the button like self.tabBarController!.navigationItem.leftBarButtonItems!.first! – Umar Farooque Mar 02 '18 at 14:58