{
//初始化UITabBarController? (可以輕松地管理多個視圖控制器)
UITabBarController *tabBarControllr = [[UITabBarController alloc] init];
//創(chuàng)建子視圖控制器
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);
}