Ben Bregman
Ben Bregman

Reputation: 21

Multiple badge colors across items in a TabBar Controller (Swift)

I have an app with a four-tabbed UITabBarController. I've been trying to customize different badge (background) colors across the various tabs, but haven't been able to achieve a unique customization per tab.

What I'm trying to accomplish:

It seems like the tab bar will only look at the standardAppearance for the tabBarItem in the currently selected tab, e.g. if i set the badgeBackgroundColor for tab 3 to blue, the badges across the whole TabBar turn blue when i select the tab. Then when i switch to any other tab, the badges across all TabBar items take on the badgeBackgroundColor from the newly selected tab.

I also tried to use tabBarItem.badgeColor but this seemed to have no effect at all.

Will the standardAppearance of a tabItem always impact all the other tabs? Is there a way to uniquely customize the badgeBackgroundColor of each tabItem across the app?

Upvotes: 2

Views: 421

Answers (2)

Geek__Lee
Geek__Lee

Reputation: 277

u can try like this:

  • first question
 selectedIndex = 2
  • second question
tabBar.selectedItem?.badgeColor = .red

Upvotes: 0

The Swift Coder
The Swift Coder

Reputation: 442

Can't speak for the second two questions, but to answer the first:

To have the second tab bar selected initially, connect the tabBar to a custom UITabBarController and use the tabBarController's selectedIndex to set the initial tab like so:

class TabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    //The second tab is the first to show
     self.selectedIndex = 1
}

Upvotes: 0

Related Questions