Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set iPhone tab bar icon in code?

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?

like image 286
Mark Struzinski Avatar asked Mar 15 '10 18:03

Mark Struzinski


People also ask

How do I create a tab bar?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


2 Answers

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.

like image 130
Ty. Avatar answered Sep 28 '22 10:09

Ty.


Yes. Your UITabBar has a property called items, which is an array of UITabBarItems for each tab bar item. You can create a UITabBarItem using the –initWithTitle:image:tag: constructor to use your own image, or the –initWithTabBarSystemItem:tag: constructor to use a system image.

like image 29
newacct Avatar answered Sep 28 '22 10:09

newacct