How to set tab bar item title programmatically in objective c?

前端 未结 10 1366
猫巷女王i
猫巷女王i 2021-01-31 15:46

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

- (IBAction)tab1Click:(id)sender {
    myTabBarController = [[UITabBarContro         


        
相关标签:
10条回答
  • 2021-01-31 16:19

    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")
    
    0 讨论(0)
  • 2021-01-31 16:24

    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 讨论(0)
  • 2021-01-31 16:25
    [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];
    
    0 讨论(0)
  • 2021-01-31 16:25
    - (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;
    }
    
    0 讨论(0)
提交回复
热议问题