6

How to change the tab bar badge size?

I set the tab bar badge value with position

tabBarController?.tabBar.items?[4].badgeValue = "1"

but I can't change the red circle tab bar badge size.

Thank you!

Jay Patel
  • 2,642
  • 2
  • 18
  • 40
Rinku Vashist
  • 355
  • 3
  • 14

1 Answers1

16

It's working

func addRedDotAtTabBarItemIndex(index: Int) {
    for subview in tabBarController!.tabBar.subviews {

        if let subview = subview as? UIView {

            if subview.tag == 1234 {
                subview.removeFromSuperview()
                break
            }
        }
    }

    let RedDotRadius: CGFloat = 5
    let RedDotDiameter = RedDotRadius * 2

    let TopMargin:CGFloat = 5

    let TabBarItemCount = CGFloat(self.tabBarController!.tabBar.items!.count)

    let screenSize = UIScreen.main.bounds
    let HalfItemWidth = (screenSize.width) / (TabBarItemCount * 2)

    let  xOffset = HalfItemWidth * CGFloat(index * 2 + 1)

    let imageHalfWidth: CGFloat = (self.tabBarController!.tabBar.items![index] as! UITabBarItem).selectedImage!.size.width / 2

    let redDot = UIView(frame: CGRect(x: xOffset + imageHalfWidth - 7, y: TopMargin, width: RedDotDiameter, height: RedDotDiameter))

    redDot.tag = 1234
    redDot.backgroundColor = UIColor.red
    redDot.layer.cornerRadius = RedDotRadius

        self.tabBarController?.tabBar.addSubview(redDot)

}
Rinku Vashist
  • 355
  • 3
  • 14