37

I want to set title to tab item programatically, but it not works. My code is below:

- (IBAction)tab1Click:(id)sender {
    myTabBarController = [[UITabBarController alloc] init];        
    view2Controller = [[View2Controller alloc] init]; 
    [view2Controller setTitle:@"title"];
    view3Controller = [[View3Controller alloc] init];  
    deneme = [[ViewController alloc] init];  

    myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme, view2Controller,view3Controller, nil]; 
    [self.view addSubview:myTabBarController.view];    
    myTabBarController.selectedIndex=1;
}
1

10 Answers 10

66

You can set all the UITabBar icons in an easy way. You can do this in your viewWillAppear: method:

[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(@"BotonMapas", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonRA", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(@"BotonEstado", @"comment")];

[[self.tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(@"LabelInfo", @"comment")];

Swift 3.1 Solution

self.tabBarController?.tabBar.items?[0].title = NSLocalizedString("BotonMapas", comment: "comment")
self.tabBarController?.tabBar.items?[1].title = NSLocalizedString("BotonRA", comment: "comment")
self.tabBarController?.tabBar.items?[2].title = NSLocalizedString("BotonEstado", comment: "comment")
self.tabBarController?.tabBar.items?[3].title = NSLocalizedString("LabelInfo", comment: "comment")
2
  • 6
    Which view controller's viewWillAppear:?
    – fatuhoku
    Feb 15, 2015 at 10:37
  • How about to set title for each an UIViewController?
    – alexmorhun
    Feb 28, 2017 at 13:28
29

How I do it in the actual View Controller (not the App Delegate):

// set tab title
self.title = @"Tab Title";
// optionally, set different title for navigation controller
self.navigationItem.title = @"Nav Title";
// Note: self.title will reset Nav Title. Use it first if you want different titles
1
  • 1
    +1 for setting a different title for the navigation controller! I needed that to make it uppercase, on the navigation bar only.
    – nmdias
    Mar 3, 2015 at 16:03
8

An easy way to do this : In your viewController2 's viewDidLoad method, set self.title = @"MyTitle";

1
  • 1
    The issue with this is that if it is in the viewDidLoad for viewcontroller2, it will only fire once viewController2's tab has been clicked on. At that point it will show correctly, but to fix it so that it shows correctly from the get-go, us: [self.tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(@"BotonMapas", @"comment")]; Like Aitul listed as his answer. Mar 16, 2013 at 20:18
5
[view2Controller setTitle:@"ImATitle"];

might be what your after

edit: ok i just tested this and it def works for me so give it a go

UINavigationController *nav1 = [[UINavigationController alloc] init]; 
myViewController *myView = [[myViewController alloc] init];
//myView.title = @"Title"; //prob not needed
[nav1 pushViewController: myView  animated:NO];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage      imageNamed:@"title.png"] tag:0];
nav1.tabBarItem = item;
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nav1, nil];
3
4

The title displayed on a given tab bar item is determined by the corresponding view controller's instance of UITabBarItem. Those aren't mutable, though... if you want to change the title (or image, or tag), you have to make a new item and assign it to the view controller.

UITabBarItem *item2 = [[UITabBarItem alloc initWithTitle:@"someTitle" image:someImage tag:0];
viewController2.tabBarItem = item2;
1
  • I try this but not worked - (IBAction)tab1Click:(id)sender { myTabBarController = [[UITabBarController alloc] init]; view2Controller = [[View2Controller alloc] init]; view3Controller = [[View3Controller alloc] init]; UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"someTitle" image:nil tag:0]; view2Controller.tabBarItem = item2; myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme,view2Controller,view3Controller, nil]; [self.view addSubview:myTabBarController.view]; myTabBarController.selectedIndex=1; } Jan 19, 2012 at 13:02
4

try this

[(UIViewController *)[tabBarController.viewControllers objectAtIndex:Index] setTitle:@"Title"]; 

or also you can set tab bar by in this way

UITabBarItem *tabItem = [[[tabBarController tabBar] items] objectAtIndex:INDEX];
[tabItem setTitle:@"TITLEe"];
1
  • Judging from the documentation, there's no way to set the title of a tab bar item after you create it. You need to make a new one instead.
    – Caleb
    Jan 19, 2012 at 12:44
2

first declare UITabBarDelegate

- (IBAction)tab1Click:(id)sender {

    myTabBarController = [[UITabBarController alloc] init]; 

    myTabBarController.delegate = self;

    view2Controller = [[View2Controller alloc] init]; 
    [view2Controller setTitle:@"title"];
    view3Controller = [[View3Controller alloc] init];  
    deneme = [[ViewController alloc] init];  
    dename.title = @"Dename";
    view2Conreoller.title = @"View2";
    view3Conreoller.title = @"View3";
    myTabBarController.viewControllers = [NSArray arrayWithObjects:deneme, view2Controller,view3Controller, nil]; 
    [self.view addSubview:myTabBarController.view];    
    myTabBarController.selectedIndex=1;
}

and even you can set tab images using

view2Controller.tabBarItem.image = [UIImage imageNamed:@"misle.png"];
0
1

If you are calling this from within an embedded UIViewController, you can change the parent tab bar title with:

self.tabBarController.title = @"My Title.";

0
1

In swift you can change the UITabBarController item's title in viewDidLoad: method as below-

   self.viewControllers![0].title="Deposit"
   self.viewControllers![1].title="Withdraw"
   self.viewControllers![2].title="Activity"
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    UITabBarController *tb=[[UITabBarController alloc]init];
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main"   bundle:nil];
    UIViewController *vc1=[sb   instantiateViewControllerWithIdentifier:@"View1"];
    UIViewController *vc2=[sb   instantiateViewControllerWithIdentifier:@"View2"];

      //To Set Title for UIViewController

      [vc1 setTitle:@"View1"];
      [vc2 setTitle:@"View2"];


    NSArray *vController=[[NSArray alloc]initWithObjects:vc1,vc2,nil];
    tb.viewControllers=vController;
    self.window.rootViewController=tb;
    [self.window makeKeyAndVisible];


  return YES;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.