TabBarItem BadgeColor

Hello, since Xcode 13 the following Code does not work anymore, it still worked with Xcode 12

 viewController.tabBarItem.badgeColor = color

In the Appearance settings, i can still set the badge color on app start.

            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

            }

In my situation, it is necessary to change the badgeColor depending on the ViewController state, so i need a code like this

 viewController.tabBarItem.badgeColor = color

Can you help me? Any ideas?

Where do you set the badgeColor ?

"viewDidLoad" or "init" (i've tried both) of my viewController. I observe a property of my viewmodel and on any change, i change the color. With Xcode 12 this works perfekt.

You can create a per-item customization - the same standardAppearance and scrollEdgeAppearance properties exist on the UITabBarItem attached to your view controller. Create and assign a UITabBarAppearance to the standardAppearance property and you can customize the badge background with viewController.tabBarItem.standardAppearance.normal.badgeBackgroundColor.

The following worked for me inside my UITabBarController:

if #available(iOS 13.0, *) {
    let appearance = tabBar.standardAppearance.copy()
    setTabBarItemBadgeAppearance(appearance.stackedLayoutAppearance)
    setTabBarItemBadgeAppearance(appearance.inlineLayoutAppearance)
    setTabBarItemBadgeAppearance(appearance.compactInlineLayoutAppearance)
    tabBar.standardAppearance = appearance
    if #available(iOS 15.0, *) {
        tabBar.scrollEdgeAppearance = appearance
}
@available(iOS 13.0, *)
private func setTabBarItemBadgeAppearance(_ itemAppearance: UITabBarItemAppearance) {
    itemAppearance.normal.badgeBackgroundColor = UIColor.green
}

Thanks! Worked as expected.

Got me wondering what if I want to have different colors for different items?

TabBarItem BadgeColor
 
 
Q