在iOS 13中,UITabBarItem的standardAppearance应用于所有其他项目

时间:2019-10-22 01:29:02

标签: ios uitabbar uitabbaritem

我正在尝试在iOS 13上做一个简单的事情-最后一个标签栏项应始终以其他颜色显示

我尝试使用新的UITabBarItem.standardAppearance成员。这是我的代码:

// 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
}

预期的行为:

所有选项卡项均以tabItem / tabItemSelected的颜色显示始终,最后一个选项卡项以{{1} } / tabItemSpecial颜色。

实际行为:

选择了除最后一项以外的任何一项-所有选项卡项均显示在tabItemSpecialSelected / tabItem中,包括最后一项! 当我选择最后一个选项卡栏项目时,也将tabItemSelected样式应用于所有其他选项卡栏项目全部!因此,所有人都使用standardAppearance / tabItemSpecial配色方案。

我做错什么了吗?或者也许我误解了新的API,并且无法让一个选项卡栏项始终具有不同的颜色?

1 个答案:

答案 0 :(得分:0)

这是预期的行为。您不是在自定义特定选项卡栏项目的外观,而是在自定义该选项卡是当前选定项时的选项卡方式。