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"; ",否则无效,如: