Swift setting Badge Value for UITabBarItem
Xcode 7.2.1 Swift 2.1.1
You just have to set the badgeValue for your desired UITabBarItem as follow:
tabBarController?.tabBar.items?[4].badgeValue = "1" // this will add "1" badge to your fifth tab bar item
// or like this to apply it to your first tab
tabBarController?.tabBar.items?.first?.badgeValue = "1st"
// or to apply to your second tab
tabBarController?.tabBar.items?[1].badgeValue = "2nd"
// to apply it to your last tab
tabBarController?.tabBar.items?.last?.badgeValue = "Last"
To remove a badge from the UITabBarItem just add nil value to it
tabBarController?.tabBar.items?.first?.badgeValue = nil
Set UITabBarItem badge value in appDelegate
Going on the assumption that your root view controller is actually the tab bar controller, you need to change:
if let tabBarController = rootViewController.tabBarController {
to:
if let tabBarController = rootViewController as? UITabBarController {
How to set the tab bar badge with swift?
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
Setting badge value in UITabBarItem in UIViewController
Yes, i got the answer.
[[self navigationController] tabBarItem].badgeValue = @"3";
Set TabBar Item badge count with SwiftUI
Currently, SwiftUI don't have badge feature so we must custom.
Reference HERE I create My tabar with badge
struct ContentView: View {
private var badgePosition: CGFloat = 2
private var tabsCount: CGFloat = 2
@State var selectedView = 0
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .bottomLeading) {
TabView {
Text("First View")
.tabItem {
Image(systemName: "list.dash")
Text("First")
}.tag(0)
Text("Second View")
.tabItem {
Image(systemName: "star")
Text("Second")
}.tag(1)
}
ZStack {
Circle()
.foregroundColor(.red)
Text("3")
.foregroundColor(.white)
.font(Font.system(size: 12))
}
.frame(width: 15, height: 15)
.offset(x: ( ( 2 * self.badgePosition) - 0.95 ) * ( geometry.size.width / ( 2 * self.tabsCount ) ) + 2, y: -30)
.opacity(1.0)
}
}
}
}
Update Tab Item Badge after getting data (Swift)
This creates
let tabBar = CustomTabBar()
a new instance instead you need
guard let tabBar = self.tabBarController as? CustomTabBar else { print("returned") ; return }
UITabBar Badge from array.count
This is the class of your view controller where you need to set badge value.
class YourViewController: UIViewController {
static weak var shared: YourViewController?
override function viewDidLoad() {
super.viewDidLoad()
YourViewController.shared = self
}
public func updateBadgeValue() {
guard let array = UserDefaults.standard.value(forKey: "yourKeyOfStoredArray") as? [Any] else { //raplace yourKeyOfStoredArray to the key you use to store the array
print("Don't have a stored array for key yourKeyOfStoredArray")
return
}
guard let items = self.tabBarController?.tabBar.items else { // Only if you use tab bar controller, if no delete this scope and uncomment next
print("Don't have tab bar controller")
return
}
/*
guard let items = self.tabBar.items else { // replace tabBar by reffence to your tab bar @IBOutlet
print("Don't have tab bar")
return
}*/
let index = 0 //<-The index of the tabbar item which you need to set badge value
if items.count > index {
items[index].badgeValue = String(array.count)
} else {
print("Don't have item at index \(index)")
}
}
}
This is the class of view controller where you need to tap a button to update badge value of YourViewController.
class AnotherViewController: UIViewController {
@IBAction func buttonPressed() {
YourViewController?.shared.updateBadgeValue()
}
}
Adjust UITabBarItem Badge position?
I started do this cycle every time, when I change a badge value.
All is fine now, except little visible shift on start.
Related Topics
Swift - Firebase Search in Database
How to Avoid Nested Navigation Bars in Swiftui
Are the #If Debug Statements Really Needed for Previews in Swiftui to Remove It in a Release Build
Adding a Case to an Existing Enum with a Protocol
How to Make Keyboard Dismiss When I Press Out of Searchbar on Swift
Binding an Element of an Array of an Observableobject:'Subscript(_:)' Is Deprecated
Is There an Alternative to Initialize() in MACos Now That Swift Has Deprecated It
Swift/Uiview/Drawrect - How to Get Drawrect to Update When Required
Swift: How to Call Cckeyderivationpbkdf from Swift
When to Use [Self] VS [Weak Self] in Swift Blocks
Set a Default Value for Uipickerview in Swift
How to Completely Remove Realm Database from iOS
Difference Between Using Objectidentifier() and '===' Operator
Implementing Nscopying in Swift with Subclasses
How to Detect When Url of Amp Page Changed with Wkwebview