隐藏选项卡栏后,UITabBarItem 字体会更改



我在应用程序中遇到了一个奇怪的问题,在使用以下选项打开视图控制器后,所选标签栏颜色从我设置的颜色变为色调颜色:

hidesBottomBarWhenPushed = true

以下是我用于在客户UITabBarController的初始化器中找到问题之前设置字体颜色的代码:

let attributes = ...
UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)

我在 StackOverflow 上找不到类似的问题,并在我公司团队的帮助下设法找到了解决方案,所以我想我会在这里分享它,以防将来对任何人有所帮助。

解决我们设法找到的上述问题的唯一方法是将以下代码添加到客户UITabBarViewController的初始值设定项

if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.backgroundColor = UIColor.white
appearance.shadowImage = UIImage()
appearance.shadowColor = UIColor.white
let defaultAttributes = ...
appearance.stackedLayoutAppearance.normal.iconColor = UIColor.red
appearance.stackedLayoutAppearance.normal.titleTextAttributes = defaultAttributes
appearance.stackedLayoutAppearance.selected.iconColor = UIColor.red
appearance.stackedLayoutAppearance.selected.titleTextAttributes = attributes
tabBar.standardAppearance = appearance
}

最新更新