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 !
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 !
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
The following line may help you to show badge in UITabBerItem
tabBarController?.tabBar.items?[your_desired_tabBer_item_number].badgeValue = value
One can also set an empty string for the badge value to get a red circle if desired:
tabBarController?.tabBar.items?.last?.badgeValue = ""
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.
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)
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
func showHideInnerBagde(value:String,indexValue:Int) {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let sceneDelegate = windowScene.delegate as? SceneDelegate,
let navController = sceneDelegate.window?.rootViewController as? UINavigationController,
let visibleController = navController.visibleViewController,
let tabbarController = visibleController as? UITabBarController,
tabbarController.tabBar.items?.indices.contains(2) ?? false {
let tabIndex = indexValue
tabbarController.tabBar.items?[tabIndex].badgeValue = value
tabbarController.tabBar.items?[tabIndex].badgeColor = UIColor.clear
let attributes: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12) // Set your desired font
]
tabbarController.tabBar.items?[tabIndex].setBadgeTextAttributes(attributes, for: .normal)
} else {
// Handle the case when the conditions are not met or the tab at index 2 does not exist.
print("Unable to set badge value and text attributes.")
}
}