【发布时间】:2019-10-22 01:29:02
【问题描述】:
我正在尝试在 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
}
预期行为:
所有标签项ALWAYS以tabItem/tabItemSelected颜色显示,最后一个标签项ALWAYS以tabItemSpecial/tabItemSpecialSelected颜色显示.
实际行为:
当除了最后一个之外选择了任何项目时 - 所有标签项都显示在tabItem / tabItemSelected,,包括最后一个!
当我选择最后一个标签栏项目时,它的standardAppearance 样式也应用于TO ALL其他标签栏项目!所以他们都使用tabItemSpecial/tabItemSpecialSelected配色方案。
我做错了吗?或者我误解了新的 API,没有办法让一个标签栏项始终具有不同的颜色?
【问题讨论】:
-
基本上我会说不要尝试使用带有标签栏的新 UIBarAppearance 东西。它破坏了标签栏的整体外观。我可以给你写一个我发现的错误列表,但是页边距太小,无法容纳它。
标签: ios uitabbar uitabbaritem