jo1995
jo1995

Reputation: 414

How to Reset the badgeValue after loading the screen in swift?

I want to reset the badge value of the tapItem if the user has seen the notification by visiting the screen.

With this code, I create the badgeValue. but it will never be reset:

func createBadgecount() {
    if let tapItems = self.tabBarController?.tabBar.items as NSArray! {
        let tapItem = tapItems[3] as! UITabBarItem
        tapItem.badgeColor = UIColor.black
        tapItem.badgeValue = "\(reports.count)"

    }
}

Thanks in advance for your help!

Upvotes: 0

Views: 80

Answers (2)

Ahmad Abbas
Ahmad Abbas

Reputation: 131

You can set value nil in

override func viewDidAppear(_ animated: Bool) {
  if let tabItem = self.tabBarController?.tabBar.selectedItem {
    tabItem.badgeValue = nil
   }
}

Upvotes: 0

Robert Dresler
Robert Dresler

Reputation: 11200

You want to set badgeValue of selectedItem in tabBar to nil if this UIViewController did appear.

So add this to viewDidAppear

override func viewDidAppear(_ animated: Bool) {
    if let tabItem = self.tabBarController?.tabBar.selectedItem {
        tabItem.badgeValue = nil
    }
}

Upvotes: 2

Related Questions