升级到 iOS 13后,之前正常显示的 tabBar 的背景色设置失效了。
解决方法如下,代码展示:
+(void)initialize {
NSDictionary *attrNormal = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kLightGrayColor};
NSDictionary *attrSelect = [NSDictionary dictionary];
UITabBar *tabBar = [UITabBar appearance];
//ios 13 之后需要这样设置才有效
if (@available(iOS 13.0, *)) {
attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor labelColor]};
UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
//设置tabar背景色
tabBarAppearance.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNormal;
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect;
//必须要加上这两句
tabBar.standardAppearance = tabBarAppearance;
if (@available(iOS 15.0, *)) {
tabBar.scrollEdgeAppearance = tabBarAppearance;
} else {
// Fallback on earlier versions
}
} else {
attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kBlackColor};
UITabBarItem *tbItem = [UITabBarItem appearance];
[tbItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
[tbItem setTitleTextAttributes:attrSelect forState:UIControlStateSelected];
[tabBar setBarTintColor:kWhiteColor]; //tabBar的背景色
}
tabBar.translucent = YES; //translucent: 半透明的
}