Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the tab bar badge with swift?

How to set the tab bar badge with swift ? for example when I get new message showing number 1 on the message icon ! Do I have to use the UITabBarItem.swift and write the code in it ! I'm not really sure how I can do it

Thank you !

like image 752
Faris Avatar asked Mar 30 '17 07:03

Faris


People also ask

What is tab bar in Swift?

The tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is, but may also be subclassed. Each tab of a tab bar controller interface is associated with a custom view controller.


1 Answers

If you got the reference to the tabBarController (e.g. from the UIViewController) you can do the following:

if let tabItems = tabBarController?.tabBar.items {     // In this case we want to modify the badge number of the third tab:     let tabItem = tabItems[2]     tabItem.badgeValue = "1" } 

From a UITabBarController it would be tabBar.items instead of tabBarController?.tabBar.items

and to delete the badge:

tabItem.badgeValue = nil 
like image 198
Lepidopteron Avatar answered Oct 11 '22 12:10

Lepidopteron