标签:
1 #import "AppDelegate.h" 2 3 @interface AppDelegate () <UITabBarControllerDelegate> 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 // Override point for customization after application launch. 12 13 // 1.创建window 14 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 15 self.window.backgroundColor = [UIColor whiteColor]; 16 [self.window makeKeyAndVisible]; 17 18 19 // 2.创建UITabBarController对象 20 UITabBarController *mainTabBar = [[UITabBarController alloc] init]; 21 22 23 // 创建控制器对象 24 UIViewController *firstVC = [[UIViewController alloc] init]; 25 firstVC.view.backgroundColor = [UIColor cyanColor]; 26 27 // 设置tabBarItem 28 // 第一种方式:系统样式 29 firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:100]; 30 31 32 // 第二种方式:自定义样式 33 UIViewController *secondVC = [[UIViewController alloc] init]; 34 secondVC.view.backgroundColor = [UIColor orangeColor]; 35 36 // 创建图片 37 UIImage *secondImg = [UIImage imageNamed:@"carGary@2x"]; 38 UIImage *selectSecondImg = [UIImage imageNamed:@"carRed@2x"]; 39 40 // 设置图片保留原有样式 41 secondImg = [secondImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 42 selectSecondImg = [selectSecondImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 43 44 secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"two" image:secondImg selectedImage:selectSecondImg]; 45 46 47 UIViewController *thirdVC = [[UIViewController alloc] init]; 48 thirdVC.view.backgroundColor = [UIColor redColor]; 49 thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"findGray@2x"] tag:101]; 50 51 52 UIViewController *fourthVC = [[UIViewController alloc] init]; 53 fourthVC.view.backgroundColor = [UIColor greenColor]; 54 55 fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"用户" image:[UIImage imageNamed:@"userGray@2x"] tag:102]; 56 57 58 UIViewController *fifthVC = [[UIViewController alloc] init]; 59 fifthVC.view.backgroundColor = [UIColor greenColor]; 60 61 fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:103]; 62 63 64 UIViewController *sixVC = [[UIViewController alloc] init]; 65 sixVC.view.backgroundColor = [UIColor greenColor]; 66 67 sixVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:104]; 68 69 70 // 设置控制器数组 71 mainTabBar.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixVC]; 72 // [mainTabBar addChildViewController:firstVC]; 73 // [mainTabBar addChildViewController:secondVC]; 74 75 // 3.将UITabBarController对象设置为window的根视图控制器 76 self.window.rootViewController = mainTabBar; 77 78 79 // 设置进入应用选中第几个 80 mainTabBar.selectedIndex = 1; 81 82 83 #pragma mark - TabBar的属性 84 // 修改字体图标等的选中颜色 85 mainTabBar.tabBar.tintColor = [UIColor greenColor]; 86 87 // 设置tabBar的半透明与否 88 mainTabBar.tabBar.translucent = NO; 89 90 // 设置tabBar的颜色 91 mainTabBar.tabBar.barTintColor = [UIColor purpleColor]; 92 93 // 改变tabBar的位置 94 [secondVC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(50, 0)]; 95 96 97 // 设置消息提示 98 thirdVC.tabBarItem.badgeValue = @"99+"; 99 100 101 // 设置代理 102 mainTabBar.delegate = self; 103 104 return YES; 105 } 106 107 108 #pragma mark - 实现代理方法 109 // 清除提示消息 110 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 111 112 viewController.tabBarItem.badgeValue = nil; 113 } 114 115 @end
标签:
原文地址:http://www.cnblogs.com/zhizunbao/p/5414607.html