Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the badge of the tab bar item is not changed

I am trying to change the badge of the tab bar item but no success. I understood that tab bar controller is responsible of controller the tab bar.

However, some content in the tab bar itself can be managed by the view controller such as the badge

in my code i tried this in view did load

self.tabBarItem.badgeValue = "3"

but nothing appears in the badge

and then I tried :

self.tabBarController?.tabBarItem.badgeValue = "3"

which didn't work neigher, well, i know why the second code didn't work, it is the same as changing the title of a navigation controllre using the navigation controller not the navigation item. but i don't know why the first code didn't work

this is the hiechy of my app, and i am doing so in the TeamsTableViewController which is the first view controller in the teams tab

enter image description here

like image 386
sarah Avatar asked May 30 '16 10:05

sarah


2 Answers

In your scenario, It is the navigation controller that controls the tabBarItem not the TableViewController, because each TabBarController has an array of viewControllers, and each one of these viewControllers is associated with a tabBarItem. In your case, the tabBarController has two viewControllers, which are:

  1. The team navigation controller
  2. The team view controller

So the team view navigation controller is the view controller that controllers the tab bar item.

Doing this should solve your problem

self.navigationController?.tabBarItem.badgeValue = "3"
like image 38
William Kinaan Avatar answered Sep 18 '22 08:09

William Kinaan


Try this

var cart: UITabBarItem = super.tabBarController.viewControllers[yourIndex].tabBarItem()
cart.badgeValue = "3"
like image 128
Sheereen S Avatar answered Sep 21 '22 08:09

Sheereen S