How to set green dot badge to a particular tabBarItem in iOS Swift?

283 Views Asked by At

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

1

There are 1 best solutions below

0
Supriyo Dey On

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