iOS升级到13.0以后,tabbar的标签文字颜色修改有了变化,变化后的解决方案如下:
UITabBarController *tb = [[UITabBarController alloc] init];
RFNewController *rvc = [RFNewController new];
RFPresentVC *rvc2 = [RFPresentVC new];
[tb addChildViewController:rvc];
[tb addChildViewController:rvc2];
rvc.title = @"控制器1";
rvc2.title = @"控制器2";
if (@available(ios 13.0,*)) {
UIColor *unselectColor = [UIColor redColor];
UIColor *selectColor = [UIColor blueColor];
UITabBarAppearance *appearance = UITabBarAppearance.new;
NSMutableParagraphStyle *par = [[NSMutableParagraphStyle alloc]init];
par.alignment = NSTextAlignmentCenter;
UITabBarItemStateAppearance *normal = appearance.stackedLayoutAppearance.normal;
if(normal) {
normal.titleTextAttributes = @{NSForegroundColorAttributeName:unselectColor,NSParagraphStyleAttributeName : par};
}
UITabBarItemStateAppearance *selected = appearance.stackedLayoutAppearance.selected;
if(selected) {
selected.titleTextAttributes = @{NSForegroundColorAttributeName:selectColor,NSParagraphStyleAttributeName : par};
}
tb.tabBar.standardAppearance = appearance;
}else{
UIColor *unselectColor = [UIColor redColor];
UIColor *selectColor = [UIColor blueColor];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:unselectColor} forState:UIControlStateNormal]; // 设置未选中颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor} forState:UIControlStateSelected];
}
效果图:

网友评论