How to set the tab bar badge with swift?

IosSwiftUitabbarcontroller

Ios Problem Overview


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 !

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

The following line may help you to show badge in UITabBerItem

tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value

Solution 3 - Ios

Set badgeValue in ViewDidAppear. Otherwise it may not appear from app loading.

import UIKit

class TabBarController: UITabBarController {

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    self.tabBar.items![2].badgeValue = "7"
}

}

No safe checks since you are in general sure that you have TabBar with n tabs.

Solution 4 - Ios

One can also set an empty string for the badge value to get a red circle if desired:

tabBarController?.tabBar.items?.last?.badgeValue = ""

enter image description here

Solution 5 - Ios

I took the @Victor code and put it in an extension to make the code smaller in the view.

import UIKit

extension UITabBar {
    func addBadge(index:Int) {
        if let tabItems = self.items {
            let tabItem = tabItems[index]
            tabItem.badgeValue = "●"
            tabItem.badgeColor = .clear
            tabItem.setBadgeTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
        }
    }
    func removeBadge(index:Int) {
        if let tabItems = self.items {
            let tabItem = tabItems[index]
            tabItem.badgeValue = nil
        }
    }
    
 }


Application: 

 tabBarController?.tabBar.addBadge(index: 1)

 tabBarController?.tabBar.removeBadge(index: 1)



Solution 6 - Ios

Thanks to @Lepidopteron, instant solution for me. In addition, you can do it with the index of selected tab index:

let tabItems = self.tabBarController?.tabBar.items as NSArray!
    var selectedIndex = tabBarController!.selectedIndex //here 
    let tabItem = tabItems![selectedIndex] as! UITabBarItem
    tabItem.badgeValue = "2"

Got the reference from this post

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFarisView Question on Stackoverflow
Solution 1 - IosLepidopteronView Answer on Stackoverflow
Solution 2 - IosRupomView Answer on Stackoverflow
Solution 3 - IosDenis KutlubaevView Answer on Stackoverflow
Solution 4 - IosDebaprio BView Answer on Stackoverflow
Solution 5 - IosGuimarãesGabrielGView Answer on Stackoverflow
Solution 6 - IosAndres PaladinesView Answer on Stackoverflow