首页 iOS13 中修改 UITabbarItem 字体颜色
文章
取消

iOS13 中修改 UITabbarItem 字体颜色

解决问题

解决 UITabbarItem 自定义字体颜色在 iOS13 中失效的问题.

问题描述

以前都通过以下代码来全局修改 UITabbarItem 标题的颜色, 当然你也可以单个分别设置.

1
2
3
4
// 未选中时候标题的颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
// 被选中后标题的颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]]} forState:UIControlStateSelected];

但是这种做法在 iOS13 上失效了. 既然失效了, 只好寻找新的解决方法了.

解决方法

最终在 stackoverflow 找到了解决方法 原文地址.

iOS13 之后, 苹果提供了新类 UITabBarAppearance 来设置 UITabBar 相关的自定义样式. 对应 UITabBarstandardAppearance 属性.

UITabBarAppearancestackedLayoutAppearance 属性类型为 UITabBarItemAppearance 就是专门用于自定义 UITabBarItem 样式的.

设置方法如下:

1
2
3
4
5
6
7
8
9
UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
// 未选中时候图标的颜色
appearance.stackedLayoutAppearance.normal.iconColor = COLOR_333333;
// 未选中时候标题颜色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : COLOR_333333};
// 选中时候标题的颜色
appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : SKIN_COLOR};

self.tabBar.standardAppearance = appearance;
本文由作者按照 CC BY 4.0 进行授权

Xcode11 的工程如何修改成老工程

UIWebView 已经彻底被苹果抛弃了