Tab Bar Controllers
The Tab Bar Interface

The Objects of a Tab Bar Interface


Creating a Tab Bar Interface

Creating a Tab Bar Interface Using a Nib File
Creating a Tab Bar Interface Programmatically
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
Creating a Tab Bar Item Programmatically
- (id)init {
if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
self.title = @"My View Controller";
UIImage* anImage = [UIImage imageNamed:@"MyViewControllerImage.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0];
self.tabBarItem = theItem;
[theItem release];
}
return self;
}
Managing Tabs at Runtime
Adding and Removing Tabs
- (IBAction)processUserInformation:(id)sender
{
// Call some app-specific method to validate the user data.
// If the custom method returns YES, remove the tab.
if ([self userDataIsValid])
{
NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[newArray removeObject:self];
[self.tabBarController setViewControllers:newArray animated:YES];
}
}
Preventing the Selection of Tabs
- (BOOL)tabBarController:(UITabBarController *)aTabBar
shouldSelectViewController:(UIViewController *)viewController
{
if (![self hasValidLogin] && (viewController != [aTabBar.viewControllers objectAtIndex:0]) )
{
// Disable all but the first tab.
return NO;
}
return YES;
}
If you need to prevent the user from selecting a tab, you can do so by providing a delegate object and implementing the tabBarController:shouldSelectViewController: method on that object. Preventing the selection of tabs should be done only on a temporary basis
Monitoring User-Initiated Tab Changes
There are two types of user-initiated changes that can occur on a tab bar:
- The user can select a tab.
- The user can rearrange the tabs.
Visual changes of that nature are best handled by your custom view controllers.
Changing a Tab’s Badge
if (self.numberOfNewItems == 0)
self.tabBarItem.badgeValue = nil;
else
self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", self.numberOfNewItems];
To assign a badge to a tab, assign a non-nil value to the badgeValue property of the corresponding tab bar item.
Tab Bar Controllers and View Rotation
Tab bar controllers support a portrait orientation by default and do not rotate to a landscape orientation unless all of the root view controllers support such an orientation. When a device orientation change occurs, the tab bar controller queries its array of view controllers. If any one of them does not support the orientation, the tab bar controller does not change its orientation.
Tab Bars and Full-Screen Layout
The tab bar controller always resizes your view to prevent it from underlapping the tab bar.