Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatic Creation and Config of UITabBarController - Setting a system Image for a tab

Creating a tabBar in an app programatically is fairly easy:

self.tabBarController = [[UITabBarController alloc] init]; 
[self.view addSubview:_tabBarController.view];

UIViewController * tab1 = [[UIViewController alloc] init];
tab1.title = "A";

UIViewController * tab2 = [[UIViewController alloc] init];
tab2.title = "B";

_tabBarController.viewControllers = [NSArray arrayWithObjects:patientSearch,todoList,nil];

[tab1 release];
[tab2 release];

You can also easily put images in the tabs:

tab1.tabBarItem.image = [UIImage imageNamed:@"myIcon.png"];

However how can i set the image of these tabs to one the system images? (eg. search, favourite, bookmarks etc.) In IB this is set by changing the 'identifier' but how can you do this programatically

like image 312
Luke Mcneice Avatar asked Jan 14 '11 16:01

Luke Mcneice


1 Answers

 UITabBarItem *aTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];

UITabBarItem docs

UITabBarSystemItem System items that can be used on a tab bar.

typedef enum {
   UITabBarSystemItemMore,
   UITabBarSystemItemFavorites,
   UITabBarSystemItemFeatured,
   UITabBarSystemItemTopRated,
   UITabBarSystemItemRecents,
   UITabBarSystemItemContacts,
   UITabBarSystemItemHistory,
   UITabBarSystemItemBookmarks,
   UITabBarSystemItemSearch,
   UITabBarSystemItemDownloads,
   UITabBarSystemItemMostRecent,
   UITabBarSystemItemMostViewed,
} UITabBarSystemItem;

To set it patientSearch.tabBarItem = aTabBarItem;

like image 175
Alex Terente Avatar answered Sep 24 '22 18:09

Alex Terente