I am at present engaged on integrating new iOS 26 options into my app, and to date, the method has been actually thrilling. Nevertheless, I’ve encountered a difficulty when updating the badge of a UIBarButtonItem, and I’m hoping to get some insights or strategies.
The app has two UIViewController cases within the navigation stack, every containing a UIBarButtonItem.
On the primary controller, the badge is ready to 1, and on the second, the badge is ready to 2. Within the second controller, there’s a “Reset” button that units the badge of the second controller to nil.
Nevertheless, after I faucet the “Reset” button, as a substitute of setting the badge to nil, it units the worth to 1.
I might respect any concepts or strategies on how one can remedy this downside. Perhaps I’m utilizing the badge API incorrectly.
Thanks!
class ViewController: UIViewController {
var cartButtonItem: UIBarButtonItem!
override func viewDidLoad() {
tremendous.viewDidLoad()
configureNavigationItem()
}
func configureNavigationItem() {
cartButtonItem = UIBarButtonItem(picture: UIImage(useful resource: .cartNavBar), type: .plain, goal: self, motion: #selector(showCart))
cartButtonItem.tintColor = UIColor.systemBlue
cartButtonItem.badge = .rely(1)
navigationItem.rightBarButtonItem = cartButtonItem
}
@objc func showCart() {
// Add second view controller in navigation stack
performSegue(withIdentifier: "Cart", sender: nil)
}
}
class CartViewController: UIViewController {
var cartButtonItem: UIBarButtonItem!
override func viewDidLoad() {
tremendous.viewDidLoad()
configureNavigationItem()
}
func configureNavigationItem() {
cartButtonItem = UIBarButtonItem(picture: UIImage(useful resource: .cartNavBar), type: .plain, goal: nil, motion: nil)
cartButtonItem.tintColor = UIColor.systemBlue
cartButtonItem.badge = .rely(2)
navigationItem.rightBarButtonItem = cartButtonItem
}
func updateBadge() {
cartButtonItem.badge = nil
}
@IBAction func resetButtonPressed(_ sender: Any) {
updateBadge()
}
}