Hello,
I hope you're all doing well! I'm currently working on integrating new iOS 26 features into my app, and so far, the process has been really exciting. However, I've encountered an issue when updating the badge of a UIBarButtonItem, and I’m hoping to get some insights or suggestions.
The app has two UIViewController instances in the navigation stack, each containing a UIBarButtonItem.
On the first controller, the badge is set to 1, and on the second, the badge is set to 2. In the second controller, there is a "Reset" button that sets the badge of the second controller to nil.
However, when I tap the "Reset" button, instead of setting the badge to nil, it sets the value to 1.
I would appreciate any ideas or suggestions on how to solve this problem. Maybe I am using the badge API incorrectly.
Thank you!
class ViewController: UIViewController {
var cartButtonItem: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationItem()
}
func configureNavigationItem() {
cartButtonItem = UIBarButtonItem(image: UIImage(resource: .cartNavBar), style: .plain, target: self, action: #selector(showCartTab))
cartButtonItem.tintColor = UIColor.systemBlue
cartButtonItem.badge = .count(1)
navigationItem.rightBarButtonItem = cartButtonItem
}
@objc func showCartTab() {
// Add second view controller in navigation stack
performSegue(withIdentifier: "Cart", sender: nil)
}
}
class CartViewController: UIViewController {
var cartButtonItem: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationItem()
}
func configureNavigationItem() {
cartButtonItem = UIBarButtonItem(image: UIImage(resource: .cartNavBar), style: .plain, target: nil, action: nil)
cartButtonItem.tintColor = UIColor.systemBlue
cartButtonItem.badge = .count(2)
navigationItem.rightBarButtonItem = cartButtonItem
}
func updateBadge() {
cartButtonItem.badge = nil
}
@IBAction func resetButtonPressed(_ sender: Any) {
updateBadge()
}
}