有时候我们需要自定义item 下面我模仿微信定制自定义的tabBar
首先 创建UITabBarController *tbc = [[UITabBarController alloc] init];
- (void)createTabBarController
{
UITabBarController *tbc = [[UITabBarControlleralloc] init];
[selfsetViewControllersFor:tbc];
//设置tabBar的背景图片
tbc.tabBar.backgroundImage = [UIImageimageNamed:@"tabbar_bg"];
NSLog(@"%@", tbc.viewControllers);
NSLog(@"%@", tbc.childViewControllers);
self.window.rootViewController = tbc;
[tbc release];
}
- (void)setViewControllersFor:(UITabBarController *)tbc
{
NSMutableArray *controllers = [NSMutableArrayarray];
NSArray *titles = @[@[@"mainframe",@"微信"],
@[@"contacts",@"通讯录"],
@[@"discover",@"发现"],
@[@"me",@"我"]];
for (NSUInteger i=0; i<titles.count; i++) {
//拼接视图控制器类名
NSString *name = [NSStringstringWithFormat:@"ViewController%lu", i];
//转换为Class变量
Class cls = NSClassFromString(name);
BaseViewController *vc = [[cls alloc] init];
//创建tabBarItem
//创建正常状态的图片
NSString *imageName = [NSStringstringWithFormat:@"tabbar_%@", titles[i][0]];
UIImage *image = [[UIImageimageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//创建选中状态的图片
NSString *selectedImageName = [imageNamestringByAppendingString:@"HL"];
UIImage *selectedImage = [[UIImageimageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *tbi = [[UITabBarItemalloc] initWithTitle:titles[i][1]image:image selectedImage:selectedImage];
因为我设置背景图片的时候用的是随机颜色 所以每个界面的颜色都不一样