4

I am not able to modify the badge color of UItabBarItem in iOS 7.1, So I just added a UILabel to TabBar and set the colour I wanted and got that working in iOS 7.1. But I am not sure if this is the correct way of doing it. I will be submitting my app to appstore. Can someone guide me if there is a chance that my app is rejected because of this? Below is my code.

UILabel *badge=[[UILabel alloc]init];
badge.text = @"2";
badge.textAlignment=NSTextAlignmentCenter;
badge.frame=CGRectMake(122, 1, 20, 20);
badge.layer.cornerRadius=10;
badge.textColor=[UIColor whiteColor];
badge.backgroundColor=[UIColor greenColor];
[tabbar addSubview:badge];
CrazyDeveloper
  • 995
  • 1
  • 13
  • 24

2 Answers2

3

You cannot modify the color of badge since it has not exposed to the user. You can only set the text as string

@property(nonatomic,copy) NSString *badgeValue;    // default is nil

What ever you are doing is fine.

Rajesh
  • 10,318
  • 16
  • 44
  • 64
0

No you can't change the color but you're doing the right thing using your own badges instead. Add this extension at the file scope and you can customise the badges however you like. Just call self.tabBarController!.setBadges([1,0,2]) in any of your root view controllers.

To be clear that is for a tab bar with three items, with the badge values going from left to right.

extension UITabBarController {
    func setBadges(badgeValues:[Int]){

        var labelExistsForIndex = [Bool]()

        for value in badgeValues {
            labelExistsForIndex.append(false)
        }

        for view in self.tabBar.subviews {
            if view.isKindOfClass(PGTabBadge) {
                let badgeView = view as! PGTabBadge
                let index = badgeView.tag

                if badgeValues[index]==0 {
                    badgeView.removeFromSuperview()
                }

                labelExistsForIndex[index]=true
                badgeView.text = String(badgeValues[index])

            }
        }

        for var i=0;i<labelExistsForIndex.count;i++ {
            if labelExistsForIndex[i] == false {
                if badgeValues[i] > 0 {
                    addBadge(i, value: badgeValues[i], color:UIColor(red: 4/255, green: 110/255, blue: 188/255, alpha: 1), font: UIFont(name: "Helvetica-Light", size: 11)!)
                }
            }
        }


    }

    func addBadge(index:Int,value:Int, color:UIColor, font:UIFont){

        let itemPosition = CGFloat(index+1)
        let itemWidth:CGFloat = tabBar.frame.width / CGFloat(tabBar.items!.count)

        let bgColor = color

        let xOffset:CGFloat = 12
        let yOffset:CGFloat = -9

        var badgeView = PGTabBadge()
        badgeView.frame.size=CGSizeMake(17, 17)
        badgeView.center=CGPointMake((itemWidth * itemPosition)-(itemWidth/2)+xOffset, 20+yOffset)
        badgeView.layer.cornerRadius=badgeView.bounds.width/2
        badgeView.clipsToBounds=true
        badgeView.textColor=UIColor.whiteColor()
        badgeView.textAlignment = .Center
        badgeView.font = font
        badgeView.text = String(value)
        badgeView.backgroundColor = bgColor
        badgeView.tag=index
        tabBar.addSubview(badgeView)

    }
}

class PGTabBadge: UILabel {

}
TimWhiting
  • 2,405
  • 5
  • 21
  • 41