UITabBarController详细说明(简介和设置)




[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     // Override point for customization after application launch.  
  5.     self.window.backgroundColor = [UIColor whiteColor];  
  6.    
  7. #pragma mark - 设置tabBarItem  
  8. #pragma mark  第一个视图ViewController  
  9.       
  10.     HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];  
  11.     // 设置A视图下----标签栏标题文字(可參照微信或者QQ体会)  
  12.     tabBarViewA.tabBarItem.title = @"微信";  
  13.     // 设置A视图下----标签栏图片(由于自己没有图片,在这里随便设置了个名字)  
  14.     //tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"];  
  15.     // 设置A视图下----标签栏信息提示(住:badgeValue是NSString类型 例如以下设置了3,就像QQ消息有3条未接受一样,给人一种提醒)  
  16.     tabBarViewA.tabBarItem.badgeValue = @"3";  
  17.     // ios7弃用了----标签栏选中的时候显示一张图片,没选中的时候显示还有一张图片  
  18.     //[tabBarViewA.tabBarItem setFinishedSelectedImage:actionMenu.selectedIcon withFinishedUnselectedImage:actionMenu.icon];  
  19.     // ios7的方法(自己没有图片,所以代码里面的图片都是一个随便取的名字,没有有用意义)  
  20.     //tabBarViewA.tabBarItem.selectedImage = actionMenu.selectedIcon;  
  21.       
  22. #pragma mark  第二个视图ViewController  
  23.     // 第二个视图ViewController  
  24.     HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];  
  25.     // 设置B视图下----标签栏  
  26.     // 用系统提供的标识(能够算等价于图标和文字)进行设置(參数:UITabBarSystemItem是个枚举值,想要什么形式,就去系统提供的API中找)  
  27.     tabBarViewB.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];  
  28.     // 设置B视图下----标签栏信息提示  
  29.     tabBarViewB.tabBarItem.badgeValue = @"GO";  
  30.       
  31. #pragma mark  第三个视图ViewController  
  32.     HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];  
  33.     tabBarViewC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];  
  34.     // 设置B视图下----标签栏信息提示  
  35.     tabBarViewC.tabBarItem.badgeValue = @"new";  
  36.       
  37. #pragma mark  第四个视图ViewController  
  38.     HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];  
  39.     tabBarViewD.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];  
  40.     // 设置B视图下----标签栏信息提示  
  41.     tabBarViewD.tabBarItem.badgeValue = @"99";  
  42.       
  43. #pragma mark  第五个视图ViewController  
  44.     HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];  
  45.     tabBarViewE.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4];  
  46.     // 设置B视图下----标签栏信息提示  
  47.     tabBarViewE.tabBarItem.badgeValue = @"sky";  
  48.       
  49. #pragma mark  第六个视图ViewController(系统默认能显示的最大视图个数是5个)  
  50.     /* 假设你的viewControllers属性加入了多于五个的items,那tab bar controller将会自己主动插入一个特殊的view controller, 
  51.     称为 More view controller,该 controller 将会负责管理多于的items,这个More view controller提供一个自己定义的界面, 
  52.     用table的方式呈现多余的view controller。而且view controller的数量是不限制的*/  
  53.     HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];  
  54.     tabBarViewF.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5];  
  55.     // 设置F视图下----标签栏信息提示  
  56.     tabBarViewF.tabBarItem.badgeValue = @"AG";  
  57.       
  58.       
  59.       
  60. #pragma mark - 设置TabBarController  
  61.       
  62.     // 创建TabBarController  
  63.     UITabBarController * tabBarController = [[UITabBarController alloc]init];  
  64.     // TabBarController默认是放在最底部的,假设你想调整位置,能够进行以下2部操作(44是iPhone中TabBarController和UINavigationController标准高度)  
  65.     //CGRect frame = CGRectMake(0, 20, 320, 44);  
  66.     //tabBarController.tabBar.frame = frame;  
  67.     // 每个tab都必须有一个content view controller------->viewControllers属性,用来存入一个应用的TabBarController有多少个界面切换  
  68.     tabBarController.viewControllers = [NSArray arrayWithObjects:tabBarViewA,tabBarViewB,tabBarViewC,tabBarViewD,tabBarViewE,tabBarViewF, nil nil];  
  69.     // 设置着色  
  70.     tabBarController.tabBar.tintColor = [UIColor greenColor];  
  71.     // 设置选中图片时候  
  72.     tabBarController.tabBar.selectedImageTintColor = [UIColor brownColor];  
  73.     // 设置背景图片(自己没有图片,不进行设置)  
  74.     //tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];  
  75.     // 设置程序启动时默认的ViewController视图(设置为3,一共5个ViewController,进来时候显示的视图就是第4个-tabBarViewD,下标从0開始)  
  76.     tabBarController.selectedIndex = 3;  
  77.       
  78.       
  79.     self.window.rootViewController = tabBarController;  
  80.       
  81.     [self.window makeKeyAndVisible];  
  82.     return YES;  
  83. }  

@最后效果例如以下图:




版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-10-01 17:39  yxwkaifa  阅读(395)  评论(0编辑  收藏  举报