How to Customize UITabBar on iOS 5
Customers love beautiful apps. Their designers make "amazing" mockups. We, developers, make their dreams come true)
I had a task: make custom TabBar in my app. In the first try - I had wrote absolutely custom TabBar with handling all taps, etc. But it was bad idea, as I understood lately.
How I'd do it now.
In iOS 5, we have so simple way to make our custom TabBar.
First of all, You have to setup the background of TabBar.
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"backgroundImgName"]];
Then, You can setup each your TabBar cell.
for(UITabBarItem *item inself.tabBar.items) {
[item setFinishedSelectedImage:[UIImage imageNamed:@"selCell"] withFinishedUnselectedImage:[UIImage imageNamed:@"stdCellImg"]];
[item setTitle: @"someText"];
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor greenColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
}
What does this code do? - It's simple!
1. Setting pics for cells
[item setFinishedSelectedImage:[UIImage imageNamed:@"selCell"] withFinishedUnselectedImage:[UIImage imageNamed:@"stdCellImg"]];
setFinishedSelectedImage: you pass img for displaying selected tab.
withFinishedUnselectedImage: you pass img for normal state of your cell (when cell hasn't chosen).
2. Set text below the image.
[item setTitle: @"someText"];
3. Set some attributes for this text (f.e. color)
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor greenColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
That's all. But, I made one more feature.
For example, in your tabs, color of text (or maybe text) changes when user select/unselect tab. Let's make it.
You have to implement protocol <UITabBarControllerDelegate>. To do it, don't forget to set self.delegate = self;
This protocol has a lot of different methods, but we need
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
This method calls when user taps on the other tab, and you can configure this transition as you want. You can even forbid transitions to some tabs in this method. Just return NO; from this method.
But, what we can do to customize out TabBar?
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
int indexOfView = [tabBarController.viewControllers indexOfObject:viewController];
UITabBarItem *item = [self.tabBar.items objectAtIndex:indexOfView];
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
// set prev item gray text
UITabBarItem *prevItem = [self.tabBar.items objectAtIndex:_prevSelectedTabIndex];
[prevItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
// change prevIndex
_prevSelectedTabIndex = indexOfView;
return YES;
}
We change color of text below the pic to red on the selected tab. And previous cell set back to gray.
That's all.
Big thanks to this article.
have a nice coding ;)















