UITabBarController的使用

本文详细介绍了如何配置和使用UITabBarController,包括设置视图控制器集合、切换视图属性、背景颜色、背景图标等,并提供了注意事项及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITabBarController 是多页面视图控制器切换控制器

一、主要使用方法,即属性设置:

1、视图控制器集合:viewControllers

2、切换视图属性:tabbar

2-1tabbar 背景颜色:backgroundColor

2-2tabbar 背景图标:backgroundImage

2-3tabbar 标题(选中,或非选中状态):UITabBarItem- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state

2-4tabbar 图标(选中,或非选中状态):UITabBarItem- (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage

或进行 2-32-4 属性的统一设置 方法如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 设置标题,未选中状态图标,选中状态图标  
  2. UITabBarItem *barItem = [[UITabBarItem alloc] initWithTitle:title image:imageNormal selectedImage:imageSelected];  
  3. xxxViewController.tabBarItem = barItem;  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 设置标题字体颜色  
  2. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];  
  3. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 设置标题字体大小  
  2. [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 设置标题字体偏移  
  2. [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 设置图标选中时颜色  
  2. [[UITabBar appearance] setTintColor:[UIColor redColor]];  

3、默认选中视图控制器:selectedIndex

     

二、使用注意事项:

1、设置视图控制器集合时,是 UINavigationController 导航栏控制器集合;

2、设置视图控制器集合时,通常小于等于 5  视图控制器,超过 5 个时系统默认生成一个 more 的控制器页面,用于操作多余的视图控制器

3、视图控制器导航栏标题设置时,注意使用:" self.navigationItem.title = @"xxx"; ",而不使用" self.title = @"xxx"; ",避免影响 UITabBarController  tabbar 标题的设置。

4、当要显示下一个视图控制器,且需要隐藏 tabbarController 控制器时,设置视图控制器的属性 hidesBottomBarWhenPushed 值为YES,如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. UIViewController *nextVC = [[UIViewController alloc] init];  
  2. nextVC.hidesBottomBarWhenPushed = YES;  
  3. [self.navigationController pushViewController:nextVC animated:YES];  

5、第一次初始化时,会出现设置的 tabbar 图标颜色异常,设置属性 tintColor 与图标颜色一致后,则不会,如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [[UITabBar appearance] setTintColor:[UIColor redColor]];  

6tabbar 标题设置后出现偏移情况,即靠近底端,可通过设置属性 titlePositionAdjustment 进行调整,如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];  

7、设置 badgeValue 标识属性时,特别是在 viewController 中设置时,注意使用方法为" self.navigationController.tabBarItem.badgeValue = @"0"; ",而不是" self.tabBarItem.badgeValue = @"0"; ",否则无效,如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (void)viewWillAppear:(BOOL)animated  
  2. {  
  3.         NSInteger index = arc4random() % 2;  
  4.         if (0 == index)  
  5.         {  
  6.             // self.tabBarItem.badgeValue = nil; // 无效  
  7.             self.navigationController.tabBarItem.badgeValue = nil;  
  8.         }  
  9.         else  
  10.         {  
  11.             index = arc4random() % 100 + 1;  
  12.             // self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", index]; // 无效  
  13.             self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", index];  
  14.         }  
  15. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 1 创建视图控制器  
  2. MessageViewController *messageVC = [[MessageViewController alloc] init];  
  3. UINavigationController *messageNav = [[UINavigationController alloc] initWithRootViewController:messageVC];  
  4. ContacterViewController *contacterVC = [[ContacterViewController alloc] init];  
  5. UINavigationController *contacterNav = [[UINavigationController alloc] initWithRootViewController:contacterVC];  
  6. DynamicViewController *dynamicVC = [[DynamicViewController alloc] init];  
  7. UINavigationController *dynamicNav = [[UINavigationController alloc] initWithRootViewController:dynamicVC];  
  8. // 视图控制器数组  
  9. NSArray *controllerArray = @[messageNav, contacterNav, dynamicNav];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 2 创建tabbarController控制器  
  2. UITabBarController *tabbarController = [[UITabBarController alloc] init];  
  3. // 属性设置  
  4. // 设置默认被选中视图控制器  
  5. tabbarController.selectedIndex = 0;  
  6. // 设置切换视图 tabBar 属性  
  7. // 1 打开用户交互  
  8. tabbarController.tabBar.userInteractionEnabled = YES;  
  9. // 2 设置背景颜色  
  10. tabbarController.tabBar.backgroundColor = [UIColor whiteColor];  
  11. // 3 设置背景图片  
  12. tabbarController.tabBar.backgroundImage = [UIImage imageNamed:@"background"];  
  13. // 选中时的背景图片  
  14. tabbarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"backgroundSelected"];  
  15. // 4 设置按钮标题、常规图标、选中时图标  
  16. NSArray *titleArray = @[@"消息"@"联系人"@"动态"];  
  17. NSArray *imageNArray = @[[UIImage imageNamed:@"messageNormal"], [UIImage imageNamed:@"contacterNormal"], [UIImage imageNamed:@"dynamicNormal"]];  
  18. NSArray *imageSAarray = @[[UIImage imageNamed:@"messageSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"dynamicSelected"]];  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**************************************/  
  2.       
  3. // 更多视图控制器的情况  
  4. UIViewController *vc4 = [[UIViewController alloc] init];  
  5. vc4.title = @"vc4";  
  6. UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];  
  7. UIViewController *vc5 = [[UIViewController alloc] init];  
  8. vc5.title = @"vc5";  
  9. UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:vc5];  
  10. UIViewController *vc6 = [[UIViewController alloc] init];  
  11. vc6.title = @"vc6";  
  12. UINavigationController *nav6 = [[UINavigationController alloc] initWithRootViewController:vc6];  
  13. UIViewController *vc7 = [[UIViewController alloc] init];  
  14. vc7.title = @"vc7";  
  15. UINavigationController *nav7 = [[UINavigationController alloc] initWithRootViewController:vc7];  
  16. UIViewController *vc8 = [[UIViewController alloc] init];  
  17. vc8.title = @"vc8";  
  18. UINavigationController *nav8 = [[UINavigationController alloc] initWithRootViewController:vc8];  
  19. // 视图控制器数组  
  20. NSArray *controllerArray = @[messageNav, contacterNav, dynamicNav, nav4, nav5, nav6, nav7, nav8];  
  21. NSArray *titleArray = @[@"消息"@"联系人"@"动态"@"nav4"@"nav5"@"nav6"@"nav7"@"nav8"];  
  22. NSArray *imageNArray = @[[UIImage imageNamed:@"messageNormal"], [UIImage imageNamed:@"contacterNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"]];  
  23. NSArray *imageSAarray = @[[UIImage imageNamed:@"messageSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"dynamicSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"]];  
  24.       
  25. /**************************************/  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 5 设置视图控制器  
  2. tabbarController.viewControllers = controllerArray;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 6 设置 tabbar 标题、图标  
  2. // 方法1  
  3. NSArray *items = tabbarController.tabBar.items;  
  4. NSInteger count = items.count;  
  5. for (int index = 0; index < count; index++)  
  6. {  
  7.         NSString *title = titleArray[index];  
  8.         UIImage *imageN = imageNArray[index];  
  9.         UIImage *imageS = imageSAarray[index];  
  10.           
  11.         UITabBarItem *aItem = [items objectAtIndex:index];  
  12.         // 标题设置(字体偏移、字体大小、字体颜色)  
  13.         aItem.title = title;  
  14. //        aItem.titlePositionAdjustment = UIOffsetMake(0.0, -10.0);  
  15. //        [aItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];  
  16. //        [aItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];  
  17. //        [aItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];  
  18.         // 图标设置  
  19. //        [aItem setFinishedSelectedImage:imageS withFinishedUnselectedImage:imageN];  
  20.         aItem.image = imageN;  
  21.         aItem.selectedImage = imageS;  
  22. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法2  
  2. UITabBarItem *messageBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[0] image:imageNArray[0] selectedImage:imageSAarray[0]];  
  3. [messageBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];  
  4. [messageBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];  
  5. messageBarItem.badgeValue = @"10";  
  6. messageVC.tabBarItem = messageBarItem;  
  7. UITabBarItem *contacterBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[1] image:imageNArray[1] selectedImage:imageSAarray[1]];  
  8. [contacterBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];  
  9. [contacterBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];  
  10. contacterBarItem.badgeValue = @"3";  
  11. contacterVC.tabBarItem = contacterBarItem;  
  12. UITabBarItem *dynamicBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[2] image:imageNArray[2] selectedImage:imageSAarray[2]];  
  13. [dynamicBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];  
  14. [dynamicBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];  
  15. dynamicBarItem.badgeValue = @"21";  
  16. dynamicVC.tabBarItem = dynamicBarItem;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 设置字体颜色  
  2. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];  
  3. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];  
  4. // 设置字体大小  
  5. [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];  
  6. // 设置字体偏移  
  7. [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];  
  8. // 设置图标选中时颜色  
  9. [[UITabBar appearance] setTintColor:[UIColor redColor]];  


   


   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值