1

I use UITabBar and UITabBarItems with it. I can set badge value for UITabBarItem before assigning tabBarItem to tabBar. But my problem is that I am not able to update the badge value of tabBarItem.

Here is code where I am able to set badge value initially:

// array of tabBarItems
NSMutableArray * tabs = [[NSMutableArray  alloc] init];

for(iterates few times)
{
    [tabs addObject:[[UITabBarItem alloc] initWithTitle:firstName image:nil tag:i]];
    // set tabItem's property
    [(UITabBarItem *)[tabs objectAtIndex:i] setFinishedSelectedImage:[self convertImage:iconImage toSize:CGSizeMake(iconWidth, TAB_ICON_HEIGHT)] withFinishedUnselectedImage:[self convertImage:iconImage toSize:CGSizeMake(iconWidth, TAB_ICON_HEIGHT)]];
    [[tabs objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", 2]];
}
// setting items of UITabBar
[self.chatTabBar setItems:tabs];

Here, I try to update the badge value. So what happens is that if I NSLog the new badge value it shows the updated value but I see no change in UI.

[[self.chatTabBar.items objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", 1]];
sth
  • 222,467
  • 53
  • 283
  • 367
Geek
  • 8,280
  • 17
  • 73
  • 137

2 Answers2

5

Try it:

UIViewController *carrinhoVC = [self.tabBarController.viewControllers objectAtIndex:0];

            carrinhoVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", 1];

carrinhoVC is the UIViewController that you want to update badge value inside TabBar.

Marckaraujo
  • 7,422
  • 11
  • 59
  • 97
1

This code worked for me:

if let tabBar = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController,
    let tabBarItem = tabBar.tabBar.items?[1] {
    tabBarItem.badgeValue = nil
}
rockdaswift
  • 9,613
  • 5
  • 40
  • 46