UITabBarController 是多页面视图控制器切换控制器
一、主要使用方法,即属性设置:
1、视图控制器集合:viewControllers
2、切换视图属性:tabbar
2-1、tabbar 背景颜色:backgroundColor
2-2、tabbar 背景图标:backgroundImage
2-3、tabbar 标题(选中,或非选中状态):UITabBarItem,- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state
2-4、tabbar 图标(选中,或非选中状态):UITabBarItem,- (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage
或进行 2-3、2-4 属性的统一设置 方法如:
- // 设置标题,未选中状态图标,选中状态图标
- UITabBarItem *barItem = [[UITabBarItem alloc] initWithTitle:title image:imageNormal selectedImage:imageSelected];
- xxxViewController.tabBarItem = barItem;
- // 设置标题字体颜色
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
- // 设置标题字体大小
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];
- // 设置标题字体偏移
- [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];
- // 设置图标选中时颜色
- [[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,如:
- UIViewController *nextVC = [[UIViewController alloc] init];
- nextVC.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:nextVC animated:YES];
5、第一次初始化时,会出现设置的 tabbar 图标颜色异常,设置属性 tintColor 与图标颜色一致后,则不会,如:
- [[UITabBar appearance] setTintColor:[UIColor redColor]];
6、tabbar 标题设置后出现偏移情况,即靠近底端,可通过设置属性 titlePositionAdjustment 进行调整,如:
- [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];
7、设置 badgeValue 标识属性时,特别是在 viewController 中设置时,注意使用方法为" self.navigationController.tabBarItem.badgeValue = @"0"; ",而不是" self.tabBarItem.badgeValue = @"0"; ",否则无效,如:
- - (void)viewWillAppear:(BOOL)animated
- {
- NSInteger index = arc4random() % 2;
- if (0 == index)
- {
- // self.tabBarItem.badgeValue = nil; // 无效
- self.navigationController.tabBarItem.badgeValue = nil;
- }
- else
- {
- index = arc4random() % 100 + 1;
- // self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", index]; // 无效
- self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", index];
- }
- }
- // 1 创建视图控制器
- MessageViewController *messageVC = [[MessageViewController alloc] init];
- UINavigationController *messageNav = [[UINavigationController alloc] initWithRootViewController:messageVC];
- ContacterViewController *contacterVC = [[ContacterViewController alloc] init];
- UINavigationController *contacterNav = [[UINavigationController alloc] initWithRootViewController:contacterVC];
- DynamicViewController *dynamicVC = [[DynamicViewController alloc] init];
- UINavigationController *dynamicNav = [[UINavigationController alloc] initWithRootViewController:dynamicVC];
- // 视图控制器数组
- NSArray *controllerArray = @[messageNav, contacterNav, dynamicNav];
- // 2 创建tabbarController控制器
- UITabBarController *tabbarController = [[UITabBarController alloc] init];
- // 属性设置
- // 设置默认被选中视图控制器
- tabbarController.selectedIndex = 0;
- // 设置切换视图 tabBar 属性
- // 1 打开用户交互
- tabbarController.tabBar.userInteractionEnabled = YES;
- // 2 设置背景颜色
- tabbarController.tabBar.backgroundColor = [UIColor whiteColor];
- // 3 设置背景图片
- tabbarController.tabBar.backgroundImage = [UIImage imageNamed:@"background"];
- // 选中时的背景图片
- tabbarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"backgroundSelected"];
- // 4 设置按钮标题、常规图标、选中时图标
- NSArray *titleArray = @[@"消息", @"联系人", @"动态"];
- NSArray *imageNArray = @[[UIImage imageNamed:@"messageNormal"], [UIImage imageNamed:@"contacterNormal"], [UIImage imageNamed:@"dynamicNormal"]];
- NSArray *imageSAarray = @[[UIImage imageNamed:@"messageSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"dynamicSelected"]];
- /**************************************/
- // 更多视图控制器的情况
- UIViewController *vc4 = [[UIViewController alloc] init];
- vc4.title = @"vc4";
- UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];
- UIViewController *vc5 = [[UIViewController alloc] init];
- vc5.title = @"vc5";
- UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:vc5];
- UIViewController *vc6 = [[UIViewController alloc] init];
- vc6.title = @"vc6";
- UINavigationController *nav6 = [[UINavigationController alloc] initWithRootViewController:vc6];
- UIViewController *vc7 = [[UIViewController alloc] init];
- vc7.title = @"vc7";
- UINavigationController *nav7 = [[UINavigationController alloc] initWithRootViewController:vc7];
- UIViewController *vc8 = [[UIViewController alloc] init];
- vc8.title = @"vc8";
- UINavigationController *nav8 = [[UINavigationController alloc] initWithRootViewController:vc8];
- // 视图控制器数组
- NSArray *controllerArray = @[messageNav, contacterNav, dynamicNav, nav4, nav5, nav6, nav7, nav8];
- NSArray *titleArray = @[@"消息", @"联系人", @"动态", @"nav4", @"nav5", @"nav6", @"nav7", @"nav8"];
- 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"]];
- 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"]];
- /**************************************/
- // 5 设置视图控制器
- tabbarController.viewControllers = controllerArray;
- // 6 设置 tabbar 标题、图标
- // 方法1
- NSArray *items = tabbarController.tabBar.items;
- NSInteger count = items.count;
- for (int index = 0; index < count; index++)
- {
- NSString *title = titleArray[index];
- UIImage *imageN = imageNArray[index];
- UIImage *imageS = imageSAarray[index];
- UITabBarItem *aItem = [items objectAtIndex:index];
- // 标题设置(字体偏移、字体大小、字体颜色)
- aItem.title = title;
- // aItem.titlePositionAdjustment = UIOffsetMake(0.0, -10.0);
- // [aItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];
- // [aItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
- // [aItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
- // 图标设置
- // [aItem setFinishedSelectedImage:imageS withFinishedUnselectedImage:imageN];
- aItem.image = imageN;
- aItem.selectedImage = imageS;
- }
- // 方法2
- UITabBarItem *messageBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[0] image:imageNArray[0] selectedImage:imageSAarray[0]];
- [messageBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
- [messageBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
- messageBarItem.badgeValue = @"10";
- messageVC.tabBarItem = messageBarItem;
- UITabBarItem *contacterBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[1] image:imageNArray[1] selectedImage:imageSAarray[1]];
- [contacterBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
- [contacterBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
- contacterBarItem.badgeValue = @"3";
- contacterVC.tabBarItem = contacterBarItem;
- UITabBarItem *dynamicBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[2] image:imageNArray[2] selectedImage:imageSAarray[2]];
- [dynamicBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
- [dynamicBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
- dynamicBarItem.badgeValue = @"21";
- dynamicVC.tabBarItem = dynamicBarItem;
- // 设置字体颜色
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
- // 设置字体大小
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];
- // 设置字体偏移
- [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];
- // 设置图标选中时颜色
- [[UITabBar appearance] setTintColor:[UIColor redColor]];