UITabBarController

  1 #import "AppDelegate.h"
  2 #import "FrisrtViewController.h"
  3 #import "SecondViewController.h"
  4 #import "ThirdViewController.h"
  5 #import "FourthViewController.h"
  6 #import "FivethViewController.h"
  7 #import "SixthViewController.h"
  8 @interface AppDelegate ()<UITabBarControllerDelegate>
  9 
 10 @end
 11 
 12 @implementation AppDelegate
 13 
 14 
 15 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 16     // Override point for customization after application launch.
 17     self.window  = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
 18     self.window.backgroundColor = [UIColor whiteColor];
 19     [self.window makeKeyAndVisible];
 20     
 21     //第一步:创建需要管理的视图对象
 22     //创建三个视图对象
 23     FrisrtViewController *firstVC = [[FrisrtViewController alloc] init];
 24     SecondViewController *secondVC = [[SecondViewController alloc] init];
 25     ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
 26     FourthViewController *fourthVC = [[FourthViewController alloc] init];
 27      FivethViewController *fivethVC = [[FivethViewController alloc] init];
 28      SixthViewController *sixthVC = [[SixthViewController alloc] init];
 29     
 30 #pragma mark - 导航控制器
 31     //创建导航控制器,控制第二个视图控制器
 32    // UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
 33     //naVC.navigationBar.barTintColor = [UIColor magentaColor];
 34     
 35     //设置tabBar的title
 36     firstVC.tabBarItem.title = @"first";
 37     secondVC.tabBarItem.title = @"second";
 38     thirdVC.tabBarItem.title = @"third";
 39     fourthVC.tabBarItem.title = @"fourth";
 40     fivethVC.tabBarItem.title = @"fiveth";
 41     sixthVC.tabBarItem.title = @"sixth";
 42     //设置tabBar的样式
 43     //参数1:是一个枚举值,系统的tabBar的样式,参数2:标记点击按钮的tag值
 44     firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:100];
 45    
 46     //设置tabBar的样式采取自定义外观
 47     //未选中状态下的图片
 48     UIImage *imageUnSelected = [UIImage imageNamed:@"carGary.png"];
 49     //图片需要保持原有图片的状态
 50     imageUnSelected = [imageUnSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 51     //选中状态下的图片设置
 52     UIImage *imageSelected = [UIImage imageNamed:@"carRed.png"];
 53     imageSelected = [imageSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 54     
 55     //设置tabBarItem的外观
 56     //参数1:设置tabBarItem的title,参数2:设置原本的image 参数3:设置选中状态下的Image
 57     secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二页" image:imageUnSelected selectedImage:imageSelected];
 58     
 59     
 60     UIImage *thirdUnSelected = [UIImage imageNamed:@"findGray.png"];
 61     thirdUnSelected = [thirdUnSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 62     UIImage *thirdSelected = [UIImage imageNamed:@"findRed.png"];
 63     thirdSelected = [thirdSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 64     thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第三页" image:thirdUnSelected selectedImage:thirdSelected];
 65     
 66     
 67     UIImage *firstUnSelectedimage = [UIImage imageNamed:@"userGray.png"];
 68     firstUnSelectedimage = [firstUnSelectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 69     UIImage *firstSelectedimage = [UIImage imageNamed:@"userRed.png"];
 70     firstSelectedimage = [firstSelectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 71     firstVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第一页" image:firstUnSelectedimage selectedImage:firstSelectedimage];
 72     
 73     
 74     UIImage *fourthUnSelectedimage = [UIImage imageNamed:@"planeGary.png"];
 75     fourthUnSelectedimage = [fourthUnSelectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 76     UIImage *fourthSelectedimage = [UIImage imageNamed:@"planeRed.png"];
 77     fourthSelectedimage = [fourthSelectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 78     fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第四页" image:fourthUnSelectedimage selectedImage:fourthSelectedimage];
 79     
 80     fivethVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:101];
 81     sixthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:102];
 82     //UITabBar默认最多显示五个按钮,如果多与五个,最后一个会出现More的标志,点击进入时剩下的controller对象
 83     
 84     //第二步:创建UITabBarController对象来管理各个视图
 85     UITabBarController *tabBarVC = [[UITabBarController alloc] init];
 86     
 87     
 88     //第三步:需要将被管理的视图控制器对象放在视图数组属性中
 89     
 90     tabBarVC.viewControllers = @[firstVC ,secondVC,thirdVC,fourthVC,fivethVC,sixthVC];
 91     //设置进入应用默认选中第几个
 92     tabBarVC.selectedIndex = 2;
 93     
 94     //
 95     UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:tabBarVC];
 96 
 97     
 98     //第四步:将window的根视图控制器交给tabBarController进行管理
 99     self.window.rootViewController = naVC;
100     
101 #pragma mark - UITabBar属性的相关设置
102     //tintColor是tabBar上内容颜色的设置(选中状态的一个颜色)
103     tabBarVC.tabBar.tintColor = [UIColor blackColor];
104     //设置tabBar的背景颜色
105     tabBarVC.tabBar.barTintColor = [UIColor cyanColor];
106     //设置tabBar的透明度
107     tabBarVC.tabBar.translucent = YES;
108 #pragma mark - UITabBarItem的一些属性的相关设置
109     
110     //设置未读消息数目
111     firstVC.tabBarItem.badgeValue = @"99";
112     secondVC.tabBarItem.badgeValue = @"未读";
113     thirdVC.tabBarItem.badgeValue = @"45";
114     fourthVC.tabBarItem.badgeValue = @"55";
115     fivethVC.tabBarItem.badgeValue = @"66";
116     sixthVC.tabBarItem.badgeValue = @"77";
117     
118 #pragma mark - 代理
119     
120     tabBarVC.delegate = self;
121     
122     //NavigationBar的默认高度是44(竖屏),TabBar的默认高度是49
123     
124 #pragma mark - UIAppearance设置外观,自iOS8之后才有的方法
125     
126     //设置全局外观
127     //通过[UITabBar appearance]找到UITabBar,从而实现设置其外观
128     //注意了:如果使用appearance去设置相应控件的外观,应该在AppDelegate中进行设置
129     [[UITabBar appearance] setBarTintColor:[UIColor redColor]];
130     [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
131     
132     //设置导航栏
133     
134     [[UINavigationBar appearance] setBarTintColor:[UIColor brownColor]];
135     [[UINavigationBar appearance] setTintColor:[UIColor lightGrayColor]];
136     
137     //设置导航栏字体
138     [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor magentaColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:18],NSFontAttributeName, nil]];
139 
140     
141     
142     return YES;
143 }
144 
145 #pragma mark - 代理方法
146 //当tabBar上面的每一个按钮被选中的时候,触发此方法
147 -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
148 {
149     viewController.tabBarItem.badgeValue = nil;
150     //查看当前那个按钮被选中
151     NSLog(@"%@",[viewController class]);
152 }

 

posted @ 2016-02-26 19:54  恒远也  阅读(263)  评论(0编辑  收藏  举报