Search code examples
iosswifttabbarbadgetabbarcontroller

iOS swift : how to set tabBar items badge values to all the tab bar items of a Tab Bar Controller


I have a Tab Bar controller with 4 tabs. I am displaying different set of data on each tab in table view. I need to set the badge values of each tab bar item with the number of rows present in the respective tab.

When the tab bar controller is loaded, the first tab is the default tab shown to the user.

Now, how can I set the badge values of all other 3 tabs with the number of records??


Solution

  • let num = "your num need show" print(num) let tabArray = self.tabBarController?.tabBar.items as NSArray! let chatTab = tabArray.objectAtIndex(3) as! UITabBarItem if num == 0{ chatTab.badgeValue = nil }else{ chatTab.badgeValue = "\(num)" }