像我這么帥的,一般都是主角哦
UITabBarController的創建步驟
UITabBarController 和UINavigationController類似(可以相互嵌套使用),UITabBarController也可以管理多個控制器,完成控制器之間的切換
步驟如下:
- 創建初始化UITabBarController
- 設置UIWindow的rootViewController為UITabBarController
- 創建相應的子控制器(viewcontroller)
- 把子控制器添加到UITabBarController
代碼示例
- 這里僅列出三個controller控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//創建UITabBarController控制器
UITabBarController *tabVc = [UITabBarController new];
//創建第一個控制器
FirstViewController *firstVc = [FirstViewController new];
//設置第一個控制器的tabBarItem的樣式
//如果圖片出現和原有樣子較大的差別,那么設置為不被渲染
UIImage *selectedImage = [UIImage imageNamed:@"carRed@2x.png"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
firstVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"車型" image:[UIImage imageNamed:@"carGary.png" ]selectedImage:selectedImage];
firstVc.tabBarItem.badgeValue=@"123";
//將視圖控制器添加到導航控制器中
UINavigationController *n1 = [[UINavigationController alloc] initWithRootViewController:firstVc];
//創建第二個視圖控制器
SecondViewController *secondVc = [SecondViewController new];
UINavigationController *n2 = [[UINavigationController alloc] initWithRootViewController:secondVc];
//設置第二個控制器的tabBarItem的樣式
secondVc.tabBarItem.title=@"聯系人";
secondVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"查找" image:[UIImage imageNamed:@"findGray.png" ]selectedImage:[UIImage imageNamed:@"findRed.png"]];
//創建第三個視圖控制器
ThirdViewController *thirdVc = [ThirdViewController new];
UINavigationController *n3 = [[UINavigationController alloc] initWithRootViewController:thirdVc];
//設置第三個控制器的tabBarItem的樣式
thirdVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"計劃" image:[UIImage imageNamed:@"planeGary.png" ]selectedImage:[UIImage imageNamed:@"planeRed.png"]];
//將所有的視圖控制器添加到tabVc的子視圖控制器中
tabVc.viewControllers = @[n1, n2, n3];
//將tabVc設置為根視圖控制器
self.window.rootViewController = tabVc;
self.window.backgroundColor = [UIColor whiteColor];
}
tabBar的常用屬性
//字體顏色
tabVc.tabBar.tintColor = [UIColor redColor];
//背景顏色
tabVc.tabBar.barTintColor = [UIColor yellowColor];
//默認選擇
tabVc.selectedIndex = 2;
//tabBar上透明度
tabVc.tabBar.translucent = YES;
*效果如圖所示
示例圖
圖片不被渲染,保持圖片本身樣式
UIImage *selectedImage = [UIImage imageNamed:@"carRed@2x.png"]; selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
實現協議中的方法(< UITabBarControllerDelegate >)
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"%ld",tabBarController.selectedIndex);
}
設置tabBar全局外觀的方法
appearance 本質是系統封裝好的單例, 小數據情況下,沒有必要使用單例,單例是全局的,它的生命周期和程序一樣長,如果使用appearance去修改響應空間的外觀,應該在APPDeleagte中進行設置,否則很容易出現無效的情況
[[UITabBar appearance] setBarTintColor:[UIColor cyanColor]];
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];