Alex Rivera | Logout

How to set iPhone tab bar icon in code?

Asked 2010-03-15T18:14:21.010
15

I am using the same ViewController for several different views.

When instantiating the ViewController for a specific view, is there an easy way to specify the tab bar icon via code?

Edit
Report

1 Answer

25
yourViewController.tabBarItem = [[UITabBarItem alloc]
initWithTitle:NSLocalizedString(@"Name", @"Name")
image:[UIImage imageNamed:@"tab_ yourViewController.png"]
tag:3];

The viewControllers are added to the tab bar, so the image and names should be set before the tab bar becomes visible (appDelegate if they are there on app start for instance). After that, you could use the above code to change the icon and text from the loadView or viewDidAppear within that viewController.

answered 2010-03-15T18:23:57.167

Your Answer