How to change uiviewcontroller name regardless of tab element name

I set my view manager title as it was in boot mode:

self.title = @"my title"; 

before that I set the title in the storyboards for the view controller and the navigation controller in which it is embedded. I installed it: "Name";

When I click on the tab containing the view controller, the title of the tab bar item and uiviewcontroller changes to: my title

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

How can i do this?

+78
ios cocoa-touch uiviewcontroller uitabbarcontroller
Feb 06 '14 at 22:39
source share
9 answers

It looks like you want to change the title in the navigation bar, but not the one in the bar. That should do it.

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

Swift:

 self.navigationItem.title = "My Title" 
+163
Feb 06 '14 at 22:45
source share

So, for those who still do not understand (like me)

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

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

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

+140
Jun 23 '15 at 13:13
source share

Swift

Customize title bar

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

Set tab item name

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

Set both headers

 self.title = "both titles" 
+9
Dec 16 '17 at 7:00
source share

For Swift, use this,

 self.navigationItem.title = "Navigation bar title" self.title = "Tab bar title" 
+7
Nov 12 '14 at 5:52
source share

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 will not affect the title if you set navigationItem.title . You will need to install tabBarItem on the navigation controller instead, so that it is taken from the controller of the tab bar.

None of the answers posted by others worked for me, because all my tab bar view controllers have navigation controllers at their root - this is a common hierarchy template for UITabBarController . tabBarItem this, you must install the tabBarItem navigation tabBarItem so that the title displays differently than the navigationItem title

You can create your tabBarItem and link them to your VC directly, like this.

  let tabBarVCOne = BooksListViewController() tabBarVCOne.tabBarItem = UITabBarItem(title: "Books", image: nil, tag: 0) tabBarViewControllers.append(tabBarVCOne) ... 

Then you will have something like this:

  //Wrap each view controller in a navigation controller. self.viewControllers = tabBarViewControllers.map(UINavigationController.init) 

But this should be changed to the following so that the tabBarItem already connected tabBarItem from the view controller and automatically set it to the navigation controller.

  self.viewControllers = tabBarViewControllers.map({ let navigationController = UINavigationController(rootViewController: $0) navigationController.tabBarItem = $0.tabBarItem return navigationController }) 

Now you can have a different header (set from your VC) separate from the header defined for your tabBarItem .

+5
Feb 10 '18 at 1:51
source share

I believe in the viewDidLoad method of the viewDidLoad controller, which you can do:

 self.title = @"my title"; self.tabBarItem.title = @"tab title"; 
0
Feb 06 '14 at 22:45
source share

Pretty late. You can use your TabBarController as a UITabBarControllerDelegate and UINavigationControllerDelegate for yourself and the navigation controllers built into each of your tabs respectively.

.h:

 @interface YourTabBarController : UITabBarController <UITabBarControllerDelegate, UINavigationControllerDelegate> @end 

.m:

 - (void) viewDidLoad { // UITabBarControllerDelegate self.delegate = self; // UINavigationControllerDelegates yourNavigationController.delegate = self; ... } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ yourNavigationController.tabBarItem.title = @"Tab Bar Title"; ... } - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { yourNavigationController.tabBarItem.title = @"Tab Bar Title"; ... } 

Based on some quick testing, it seems that these two delegate actions should cover any free cases and will update the title whether you switch tabs or view in your navigation controller. For completeness, you can update your title in didShowViewController , but based on what I saw, I don't think it is necessary.

0
09 Oct '14 at 15:08
source share

Probably a little late (but).

Setting the VC header changes the navigation header AND tabBar. (if the VC is already bound to both).

If you want to have separate headers, you need to set them manually, you usually set the header for VC, and then, in particular, the name tabBarItem, since this property

0
Jan 15 '15 at 10:17
source share

Swift 4.2

Here, please, I created an extension for the 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") } 
0
Jan 26 '19 at 18:43
source share



All Articles