在iOS 13升级之后,发现公司APP的UITabBarController上的所有小标题颜色发生异常。
对比之后,发现在iOS 12系统下,这些标题文字的颜色仍然是设置的红色,而在iOS 13中,表现为常见的系统默认蓝。
原来的写法:
viewController.tabBarItem.title = title;
[viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:246.0 / 255.0 green:92.0 / 255.0 blue:92.0 / 255.0 alpha:1.0]} forState:UIControlStateSelected];
经过Google和查看官方文档,在iOS 13中,Apple将设置UITabBarItem的文字属性的任务,交给了13中新添加的属性UITabBarItem.standardAppearance。
下面是适配iOS 13的写法:
viewController.tabBarItem.title = title;
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
// 设置未被选中的颜色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
// 设置被选中时的颜色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:246.0 / 255.0 green:92.0 / 255.0 blue:92.0 / 255.0 alpha:1.0]};
viewController.tabBarItem.standardAppearance = appearance;
} else {
[viewController.tabBarItem setTitleTextAttributes:@{NSForegro

在iOS13升级后,解决公司APP中UITabBarController标题颜色异常问题,介绍如何使用UITabBarItem.standardAppearance进行适配,并分享在不同添加方式下设置生效的情况。
最低0.47元/天 解锁文章
1万+





