Building the new version of the app Blocos de Rua I was challenged to customize the UITabBar
so it meets what the designer wants. In iOS 5 this is pretty easy to do but I haven’t figured out the proper way to do it from the beginning, this post is my findings on how to do it properly by correctly using the new iOS 5 APIs to customize appearance.
The final look:
Keep reading to see the code.
The appearance APIs in iOS 5 are great. They reduce lots of custom drawRect:
that used to be necessary to customize the default UIKit components. The first time I tried to customized the tab bar I had some problems with images been offset upwards because I was using the wrong methods.First thing I learned, the setFinishedSelectedImage:finishedUnselectedImage:
from UITabBarItem
is the tab’s icon not a image for the whole tab with background, icon and label.
Customize the UITabBar
is a peace of cake when you understand how the APIs should be used, take a look:
First - usually in your app delegate - set the image for the entire tab bar’s background, which represents the “normal” state of all tabs. The dimension is 320 x 49 points.
[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:@"background"]];
[[[self tabBarController] tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"selected"]];
Unlike the default behavior the image shouldn’t change when the tab is selected, this is why I set the same image in both states. For each UIViewController
that will be part of the tab bar you need to configure the tab image like this:
- (id)init { self = [super initWithNibName:@"MyNibName" bundle:nil]; if (self) { self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"The Title" image:nil tag:0]; [[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:@"tab_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_icon"]]; } }
UITextAttributeTextColor
key:
// below the setFinishedSelectedImage:withFinishedUnselectedImage: [[self tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
转帖:http://felipecypriano.com/2012/02/27/how-to-customize-uitabbar-on-ios-5/