Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change tab bar badge size in Swift

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!

like image 777
Rinku Vashist Avatar asked Jan 05 '18 08:01

Rinku Vashist


1 Answers

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)

}
like image 176
Rinku Vashist Avatar answered Oct 06 '22 08:10

Rinku Vashist