UITabBarItem
- 父类是UIBarItem
- UITabBarController(标签控制器)的UITabBar(标签条)的模型,设置了它,底层会根据这个模型设置UITabBar的UITabBarButton的内容。
UITabBarItem
- UITabBarItem有以下属性影响着UITabBarButton的内容
// 标题文字 nav.tabBarItem.title = title; // 图标 nav.tabBarItem.image = (imageName.length)?[UIImage imageNamed:imageName]:nil; // 选中时的图标 //IOS7之后,默认按钮被选中,则图片会被渲染成蓝色;以前的不会 nav.tabBarItem.selectedImage = (selectedImageName.length)?[UIImage imageNamed:selectedImageName]:nil; // 提醒数字 // 若有图标,则提醒数字在图标右方,否则在左方;数字最多建议为99+ nav.tabBarItem.badgeValue = @"99+";
设置TabBarItem的文字属性
文字属性的设置详见富文本
-
方案一:直接设置每一个tabBarItem对象(不推荐)
// 普通状态下的文字属性 NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary]; normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; [vc.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal]; // 选中状态下的文字属性 NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; [vc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected]; // 字典中用到的key 1.iOS7之前(在UIStringDrawing.h中可以找到) - 比如UITextAttributeFont\UITextAttributeTextColor - 规律:UITextAttributeXXX 2.iOS7开始(在NSAttributedString.h中可以找到) - 比如NSFontAttributeName\NSForegroundColorAttributeName - 规律:NSXXXAttributeName
-
方案二:通过UITabBarItem的appearance对象统一设置(推荐)
#pragma mark - initilize + (void)initialize { [super initialize]; // 判断是否是该类本身,子类也返回 if(self != [BSTabBarController class])return; // 获取tabBarItem外观 UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil]; // 普通状态 NSMutableDictionary *normalDict = [NSMutableDictionary dictionary]; normalDict[NSFontAttributeName] = [UIFont systemFontOfSize:titleFont];//字体 normalDict[NSForegroundColorAttributeName] = [UIColor grayColor];//颜色 [item setTitleTextAttributes:normalDict forState:UIControlStateNormal]; // 选中状态 NSMutableDictionary *selectedDict = [NSMutableDictionary dictionary]; selectedDict[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; [item setTitleTextAttributes:selectedDict forState:UIControlStateSelected]; }