0

Currently, I'm trying to set the green dot this way, but end result is always a red badge.

        xxxx.tabBarItem.badgeColor = .clear
        xxxx.tabBarItem.badge value = "●"

        
        let attribute : [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.green]

        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .disabled)
        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .selected)
        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .highlighted)
        xxxx.tabBarItem.setBadgeTextAttributes(attribute, for: .normal)

What do I want?

enter image description here

But code result is: enter image description here

Sunil Targe
  • 7,251
  • 5
  • 49
  • 80
  • 1
    What happens if you set `badgeColor = .green` instead of clear? – HangarRash May 02 '23 at 16:37
  • 1
    Where are you setting all of these tabBarItem properties in your code? Which method? – Rob C May 02 '23 at 17:57
  • 1
    The default value of badgeColor is systemRed. So something tells me you aren't configuring your tabBarItem in the right place. Or maybe you are doing it multiple times with different values. – Rob C May 02 '23 at 18:02
  • @HangarRash same result (it takes default red) – Sunil Targe May 03 '23 at 02:45
  • @RobC I'm setting those in `func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)` – Sunil Targe May 04 '23 at 16:13
  • I think I know what's happing, it's all because I was doing `self.tabBar.standardAppearance = tabBarAppearance` – Sunil Targe May 08 '23 at 08:34

1 Answers1

0

It will work. Use tabBar.items instead of tabBarItem.badgeValue.

 self.tabBarController?.tabBar.items?[0].badgeColor = .clear

 self.tabBarController?.tabBar.items?[0].badgeValue = "●"

 let attribute : [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : UIColor.green]

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .disabled)

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .selected)

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .highlighted)

 self.tabBarController?.tabBar.items?[0].setBadgeTextAttributes(attribute, for: .normal)

ScreenShot

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
Supriyo Dey
  • 230
  • 1
  • 12