1. 统一设置UITabBarController管理的所有VC的tabBarItem图标文字的颜色大小等属性 //设置字体大小及颜色(可变字典才可用[]方法设值) NSMutableDictionary *attr = [NSMutableDictionary dictionary]; attr[NSFontAttributeName] = [UIFont systemFontOfSize:19]; attr[NSForegroundColorAttributeName] = [UIColor grayColor]; NSMutableDictionary *selectAttr = [NSMutableDictionary dictionary]; selectAttr[NSFontAttributeName] = [UIFont systemFontOfSize:5];//NSFontAttributeName是个const变量,一旦设置了19,后面就不能再改了,这里改为5无效 selectAttr[NSBackgroundColorAttributeName] = [UIColor darkGrayColor]; UITabBarItem *item = [UITabBarItem appearance]; [item setTitleTextAttributes:attr forState:UIControlStateNormal]; [item setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted]; 2. 就不用像下面这样单独给每一个VC.tabBarItem设置字体大小和颜色了 [vc01.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc01.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted]; [vc02.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc02.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted]; [vc03.tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal]; [vc03.tabBarItem setTitleTextAttributes:selectAttr forState:UIControlStateHighlighted];
原理是:
setTitleTextAttributes:forState: 函数后面有备注UI_APPEARANCE_SELECTOR,
可以通过 [Class appearance] setTitleTextAttributes: forState: ]; 来同一设置字体颜色。
//设置UITabBarItem:UIBarItem的颜色属性 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance> - (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; - (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR
时间: 2024-10-07 18:20:06