{
//初始化UITabBarController (可以轻松地管理多个视图控制器)
UITabBarController *tabBarControllr = [[UITabBarController alloc] init];
//创建子视图控制器
FirstViewController *firstVC = [[FirstViewController alloc] init];
//设置标题
firstVC.tabBarItem.title=@"首页";
//设置图片---没选中时显示的图片
firstVC.tabBarItem.image= [UIImage imageNamed:@"tabbar_home"];
firstVC.tabBarItem.selectedImage= [UIImage imageNamed:@"tabbar_home_selected"];
//设置提示信息的红色小圆点
firstVC.tabBarItem.badgeValue=@"3";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstVC.navigationItem.title= firstVC.tabBarItem.title;
//将子视图控制器添加到tabBarControllr
//第一种方式
[tabBarControllr addChildViewController:nav];
[tabBarControllr addChildViewController:secondVC];
[tabBarControllr addChildViewController:thirdVC];
[tabBarControllr addChildViewController:fourVC];
[tabBarControllr addChildViewController:fifthVC];
//第二种方式按显示顺序放置
tabBarControllr.viewControllers= @[nav,secondVC,thirdVC,fourVC,firstVC];
//为tabBarControllr选中设置颜色
tabBarControllr.tabBar.tintColor= [UIColorcolor WithRed:253 / 255.0green:128 / 255.0blue:7 / 255.0alpha:1];
//设置tabBarControllr为根视图控制器
[UIApplicationshared Application].keyWindow.rootViewController= tabBarControllr;
//设置默认显示视图控制器页面从0开始
tabBarControllr.selectedIndex= 3;
//拼接上下层视图方式
UIImage *image1 = [UIImage imageNamed:@"tabbar_compose_button"];
UIImage *image2 = [UIImage imageNamed:@"tabbar_compose_icon_add"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(71, 49),NO, 0.0);
[image1 drawInRect:CGRectMake(0, 0, 71, 49)];
[image2 drawInRect:CGRectMake(23, 12, 25, 25)];
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsGetCurrentContext();
ThirdViewController *thirdVC= [[ThirdViewController alloc]init];
//使用原始图片
thirdVC.tabBarItem.image= [imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
thirdVC.tabBarItem.selectedImage= [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//设置图片偏移量
thirdVC.tabBarItem.imageInsets=UIEdgeInsetsMake(6, 0, -6, 0);
}
网友评论