Reputation: 3857
I'm trying to do a simple thing on iOS 13 - the last tab bar item should always be displayed in a different color.
I tried to use the new UITabBarItem.standardAppearance
member. Here are my codes:
// first, set the default colors for the whole tab bar
let color = Style.Color.tabItem
let text = [NSAttributedString.Key.foregroundColor: color]
let selectedColor = Style.Color.tabItemSelected
let selectedText = [NSAttributedString.Key.foregroundColor: selectedColor]
let barAppearance = UITabBarItemAppearance()
barAppearance.normal.iconColor = color
barAppearance.disabled.iconColor = color
barAppearance.selected.iconColor = selectedColor
barAppearance.focused.iconColor = selectedColor
barAppearance.normal.titleTextAttributes = text
barAppearance.disabled.titleTextAttributes = text
barAppearance.selected.titleTextAttributes = selectedText
barAppearance.focused.titleTextAttributes = selectedText
tabBar.standardAppearance.stackedLayoutAppearance = barAppearance
tabBar.standardAppearance.inlineLayoutAppearance = barAppearance
tabBar.standardAppearance.compactInlineLayoutAppearance = barAppearance
tabBar.standardAppearance.backgroundColor = Style.Color.tabBar
// now, for the last item set special colors
if let lastItem = tabBar.items?.last {
let specialColor = Style.Color.tabItemSpecial
let specialText = [NSAttributedString.Key.foregroundColor: specialColor]
let specialSelectedColor = Style.Color.tabItemSpecialSelected
let specialSelectedText = [NSAttributedString.Key.foregroundColor: specialSelectedColor]
let itemAppearance = UITabBarItemAppearance()
itemAppearance.normal.iconColor = specialColor
itemAppearance.disabled.iconColor = specialColor
itemAppearance.selected.iconColor = specialSelectedColor
itemAppearance.focused.iconColor = specialSelectedColor
itemAppearance.normal.titleTextAttributes = specialText
itemAppearance.disabled.titleTextAttributes = specialText
itemAppearance.selected.titleTextAttributes = specialSelectedText
itemAppearance.focused.titleTextAttributes = specialSelectedText
let itemBarAppearance = UITabBarAppearance()
itemBarAppearance.stackedLayoutAppearance = itemAppearance
itemBarAppearance.inlineLayoutAppearance = itemAppearance
itemBarAppearance.compactInlineLayoutAppearance = itemAppearance
itemBarAppearance.backgroundColor = Style.Color.tabBar
lastItem.standardAppearance = itemBarAppearance
}
All the tab items are ALWAYS shown in tabItem
/tabItemSelected
colors, the last tab item is ALWAYS shown in tabItemSpecial
/tabItemSpecialSelected
color.
When any of the items is selected except the last one - all the tabs items are shown in tabItem
/tabItemSelected
, including the last one!
When I select the last tab bar item, it's standardAppearance
style is applied TO ALL the other tab bar items too! So all of them use tabItemSpecial
/tabItemSpecialSelected
color scheme.
Am I doing something wrong? Or maybe I misunderstood the new APIs and there is no way to have one tab bar item to always have a different color?
Upvotes: 6
Views: 3253
Reputation: 151
This is the expected behavior. You aren't customizing how a specific tab bar item looks, you are customizing how the tab bar appears when that item is the currently selected one.
Upvotes: 1