4

Is it possible to change the tab bar badge color of an Objective-C iPhone app built from Xcode? It always comes in red. Can anyone direct me to sort it out?

Thanks

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
Ahmed.S.Alkaabi
  • 243
  • 2
  • 10

4 Answers4

5

The easiest way to set the color of badge post iOS 10

if #available(iOS 10, *) 
{
    UITabBarItem.appearance().badgeColor = .green
}
Kunal Kumar
  • 1,722
  • 1
  • 17
  • 32
4

For people using Swift:

 extension UITabBarController {

  func setBadges(badgeValues: [Int]) {

        for view in self.tabBar.subviews {
            if view is CustomTabBadge {
                view.removeFromSuperview()
            }
        }

        for index in 0...badgeValues.count-1 {
            if badgeValues[index] != 0 {
                addBadge(index, value: badgeValues[index], color:UIColor(paletteItem: .Accent), font: UIFont(name: Constants.ThemeApp.regularFontName, size: 11)!)
            }
        }
    }

    func addBadge(index: Int, value: Int, color: UIColor, font: UIFont) {
        let badgeView = CustomTabBadge()

        badgeView.clipsToBounds = true
        badgeView.textColor = UIColor.whiteColor()
        badgeView.textAlignment = .Center
        badgeView.font = font
        badgeView.text = String(value)
        badgeView.backgroundColor = color
        badgeView.tag = index
        tabBar.addSubview(badgeView)

        self.positionBadges()
    }

    override public func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.tabBar.setNeedsLayout()
        self.tabBar.layoutIfNeeded()
        self.positionBadges()
    }

    // Positioning
    func positionBadges() {

        var tabbarButtons = self.tabBar.subviews.filter { (view: UIView) -> Bool in
            return view.userInteractionEnabled // only UITabBarButton are userInteractionEnabled
        }

        tabbarButtons = tabbarButtons.sort({ $0.frame.origin.x < $1.frame.origin.x })

        for view in self.tabBar.subviews {
            if view is CustomTabBadge {
                let badgeView = view as! CustomTabBadge
                self.positionBadge(badgeView, items:tabbarButtons, index: badgeView.tag)
            }
        }
    }

    func positionBadge(badgeView: UIView, items: [UIView], index: Int) {

        let itemView = items[index]
        let center = itemView.center

        let xOffset: CGFloat = 12
        let yOffset: CGFloat = -14
        badgeView.frame.size = CGSizeMake(17, 17)
        badgeView.center = CGPointMake(center.x + xOffset, center.y + yOffset)
        badgeView.layer.cornerRadius = badgeView.bounds.width/2
        tabBar.bringSubviewToFront(badgeView)
    }
}

class CustomTabBadge: UILabel {}
Alexis C.
  • 4,898
  • 1
  • 31
  • 43
  • Code works but you have to order tabbarItems by position: tabbarButtons.sort({ $0.frame.origin.x < $1.frame.origin.x }) – agfa555 Mar 25 '16 at 07:12
2

Well seems like apple saw the need for this, and in iOS 10 you can now use the UITabBarItem's UIColor *badgeColor property.

Shaked Sayag
  • 5,712
  • 2
  • 31
  • 38
  • It works, but remember provide back compatibility for users who use iOS 9 and previous versions. – Yao Li Jan 12 '17 at 18:01
-1

A more robust solution points here UITabbarItem-CustomBadge.

Demo

enter image description here

This two line of code, can get u started

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  //supplying the animation parameter
  [UITabBarItem setDefaultAnimationProvider:[[DefaultTabbarBadgeAnimation alloc] init]];
  [UITabBarItem setDefaultConfigurationProvider:[[DefaultSystemLikeBadgeConfiguration alloc] init]];

  //rest of your code goes following...

  return YES;
}
Community
  • 1
  • 1
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • before putting a -1 please put a comment explaining the issues with the answer – Ratul Sharker Nov 22 '16 at 16:02
  • 1
    I've removed my downvote on this and the other answer. Your initial answer was nothing more than a link to your repository, without the demo, that's why you got downvotes. – user247702 Nov 24 '16 at 23:38