嗨,由于 Xcode 13 以下代码不再适用,它仍然适用于 Xcode 12
viewController.tabBarItem.badgeColor = color
在外观设置中,我仍然可以使用以下代码在应用开始时设置徽章颜色:
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithOpaqueBackground()
tabBarAppearance.backgroundColor = UIColor.tabBarBackground\
let tabBarItemAppearance = UITabBarItemAppearance()
tabBarItemAppearance.normal.titleTextAttributes = [.font: FontFamily.Arial.regular.font(size: 10.0)!]
tabBarItemAppearance.normal.badgeTextAttributes = [.foregroundColor: UIColor.white, .font: FontFamily.Arial.regular.font(size: 10.0)!]
tabBarItemAppearance.normal.badgeBackgroundColor = UIColor.black
tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
UITabBar.appearance().standardAppearance = tabBarAppearance
if #available(iOS 15.0, *) {
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
在我的情况下,有必要根据 ViewController 状态更改badgeColor,所以我需要这样的代码
viewController.tabBarItem.badgeColor = color