工程一如下:
编辑AppDelegate.m如下:
// // AppDelegate.m // UITabBarController工具栏 // // Created by apple on 15/9/7. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; // 初始化UITabBarController 即创建工具栏控制器 UITabBarController *tb = [[UITabBarController alloc] init]; // 设置控制器为WIndow的根控制器 self.window.rootViewController = tb; // 创建子控制器 ViewController *vc = [[ViewController alloc] init]; vc.view.backgroundColor = [UIColor grayColor]; vc.tabBarItem.title = @"工作"; vc.tabBarItem.image = [UIImage imageNamed:@"工作icon"]; vc.tabBarItem.badgeValue = @"123"; ViewController *vc1 = [[ViewController alloc] init]; vc1.view.backgroundColor = [UIColor greenColor]; vc1.tabBarItem.title = @"沟通"; vc1.tabBarItem.image = [UIImage imageNamed:@"沟通icon"]; vc1.tabBarItem.badgeValue = @"456"; // 图标上的小标题 ViewController *vc2 = [[ViewController alloc ] init]; vc2.view.backgroundColor = [UIColor redColor]; vc2.tabBarItem.title = @"通讯录"; vc2.tabBarItem.image = [UIImage imageNamed:@"通讯录icon"]; // 把子控制器加到UITabBarController控制器中 // 方式一: // [tb addChildViewController:vc]; // [tb addChildViewController:vc1]; // [tb addChildViewController:vc2]; // 方式二: tb.viewControllers = @[vc, vc1, vc2]; [self.window makeKeyAndVisible];// 是窗口可见 return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end新建一个视图控制器如下:
viewController.h
viewController.m
运行结果如下:
------------------------------------------------------------------------------------------------------------------------------
新建第二个工程如下:
依次编辑相应文件如下:
1、
// // AppDelegate.m // UITabBarController深入了解 // // Created by apple on 15/9/7. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "AppDelegate.h" #import "ViewControllerOne.h" #import "ViewControllerTwo.h" #import "ViewControllerThree.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; // 初始化一个tabBar控制器 UITabBarController *tab = [[UITabBarController alloc] init]; tab.tabBar.barTintColor = [UIColor whiteColor]; // 设置根控制器为tabBar控制器 self.window.rootViewController = tab; // 在以下创建子控制器 // ------------------------------------------------------------------------------------------------- // 创建一个用来初始化UINavigationController控制器的视图控制器 即一个导航控制器的根视图控制器 ViewControllerOne *vc1 = [[ViewControllerOne alloc] init]; UINavigationController * naviVC = [[UINavigationController alloc] initWithRootViewController:vc1]; naviVC.tabBarItem.title = @"工作"; naviVC.tabBarItem.image = [UIImage imageNamed:@"工作icon"]; vc1.tabBarItem.badgeValue = @"工作努力"; // 设置正常状态下按钮的属性——由一个字典设置 [naviVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal]; UIImage *img = [UIImage imageNamed:@"工作icon.png"]; [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]; naviVC.tabBarItem.image = img; naviVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"工作icon2"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; // 设置选择状态的按钮的属性——由一个字典设置 [naviVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected] ; //------------------------------------------------------------------------------------------------ // 新建第二个视图控制器对象 ViewControllerTwo *vc2 = [[ViewControllerTwo alloc] init]; vc2.view.backgroundColor = [UIColor redColor]; // 创建第二个视图控制器的导航控制器 UINavigationController *naviVC2 = [[UINavigationController alloc] initWithRootViewController:vc2]; // 设置导航控制器的tabBar属性 naviVC2 .tabBarItem.title = @"沟通"; naviVC2.tabBarItem.image = [UIImage imageNamed:@"沟通icon.png"]; naviVC2.tabBarItem.selectedImage = [[UIImage imageNamed:@"沟通icon2"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]; // 设置选中图片为原始样式 // 设置正常状态与选中下的属性 [naviVC2.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal]; [naviVC2.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected]; //------------------------------------------------------------------------------------------------ // 新建第三个视图控制器对象 ViewControllerThree *vc3 = [[ViewControllerThree alloc] init]; vc3.view.backgroundColor = [UIColor yellowColor]; // 创建第三个视图的导航控制器 UINavigationController *naviVC3 = [[UINavigationController alloc] initWithRootViewController:vc3]; naviVC3.tabBarItem.title = @"通讯录"; naviVC3.tabBarItem.image = [UIImage imageNamed:@"通讯录icon"]; naviVC3.tabBarItem.selectedImage = [[UIImage imageNamed:@"通讯录icon2"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] ; // 将所有绑定视图控制器的导航控制器添加到tabBar控制器中 tab.viewControllers = @[naviVC, naviVC2,naviVC3]; [self.window makeKeyAndVisible]; return YES; } -(void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item // 点击工具栏按钮时触发监听 { NSLog(@"点击工具栏按钮"); } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end依次新建列表中的控制器即可:
运行结果如下: