是否可以在 tvOS 的 uitabbaritems 上对聚焦和选择状态使用不同的颜色?
Is it possible to use different colors for focused and selected state on uitabbaritems at tvOS?
在我们的 tvOS 应用程序中,我们有一个自定义的标签栏。现在我们还想更改 focused/selected 项目的背景颜色。当我按 tabBarAppearance.selectionIndicatorTintColor = .purple
执行时,它会将焦点和选定状态更改为紫色(如果没有该行代码,我们确实有不同的颜色或至少不同的不透明度)。
没有自定义颜色:
未选择自定义颜色
自定义颜色集中
选择自定义颜色
是否可以对聚焦和选择使用不同的颜色(就像我对项目文本所做的那样,如您在屏幕截图中所见)?
我通过在 UITabBarController
中每次焦点改变时设置选项卡栏的 standardAppearance
来解决这个问题。相关代码如下所示(外观的初始设置仅为了完整性而发布):
// setting up standard appearance for the first time
private func setupTabbarAppearance() {
let tabBarAppearance = UITabBarAppearance()
//...
tabBarAppearance.selectionIndicatorTintColor = .focusedBackgroundColor // focused items
//...
let itemAppearance = UITabBarItemAppearance()
//...
itemAppearance.normal.titleTextAttributes[.foregroundColor] = .normalTextColor // used for focused AND non-focused items,
// when the whole tabbar is focused
//...
itemAppearance.selected.titleTextAttributes[.foregroundColor] = .selectedTextColor // used for the selected item,
// wen tabbar is not focused
// ...
tabBarAppearance.inlineLayoutAppearance = itemAppearance
tabBar.standardAppearance = tabBarAppearance
tabBar.setNeedsLayout()
}
private func setTabBarIndicatorColor(tabBarFocused: Bool) {
let currentAppearance = tabBar.standardAppearance
// here is where the color is set
currentAppearance.selectionIndicatorTintColor = tabBarFocused ? .focusedBackgroundColor : .selectedBackgroundColor
tabBar.standardAppearance = currentAppearance
}
// change appearance each time, when focus changes in tabbar controller
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if isTabbarInHierarchy(view: context.nextFocusedView) {
setTabBarIndicatorColor(tabBarFocused: true)
} else {
setTabBarIndicatorColor(tabBarFocused: false)
}
super.didUpdateFocus(in: context, with: coordinator)
}
private func isTabbarInHierarchy(view: UIView?) -> Bool {
guard let view = view else {return false}
if view == tabBar {
return true
}
return isTabbarInHierarchy(view: view.superview)
}
在我们的 tvOS 应用程序中,我们有一个自定义的标签栏。现在我们还想更改 focused/selected 项目的背景颜色。当我按 tabBarAppearance.selectionIndicatorTintColor = .purple
执行时,它会将焦点和选定状态更改为紫色(如果没有该行代码,我们确实有不同的颜色或至少不同的不透明度)。
没有自定义颜色:
是否可以对聚焦和选择使用不同的颜色(就像我对项目文本所做的那样,如您在屏幕截图中所见)?
我通过在 UITabBarController
中每次焦点改变时设置选项卡栏的 standardAppearance
来解决这个问题。相关代码如下所示(外观的初始设置仅为了完整性而发布):
// setting up standard appearance for the first time
private func setupTabbarAppearance() {
let tabBarAppearance = UITabBarAppearance()
//...
tabBarAppearance.selectionIndicatorTintColor = .focusedBackgroundColor // focused items
//...
let itemAppearance = UITabBarItemAppearance()
//...
itemAppearance.normal.titleTextAttributes[.foregroundColor] = .normalTextColor // used for focused AND non-focused items,
// when the whole tabbar is focused
//...
itemAppearance.selected.titleTextAttributes[.foregroundColor] = .selectedTextColor // used for the selected item,
// wen tabbar is not focused
// ...
tabBarAppearance.inlineLayoutAppearance = itemAppearance
tabBar.standardAppearance = tabBarAppearance
tabBar.setNeedsLayout()
}
private func setTabBarIndicatorColor(tabBarFocused: Bool) {
let currentAppearance = tabBar.standardAppearance
// here is where the color is set
currentAppearance.selectionIndicatorTintColor = tabBarFocused ? .focusedBackgroundColor : .selectedBackgroundColor
tabBar.standardAppearance = currentAppearance
}
// change appearance each time, when focus changes in tabbar controller
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if isTabbarInHierarchy(view: context.nextFocusedView) {
setTabBarIndicatorColor(tabBarFocused: true)
} else {
setTabBarIndicatorColor(tabBarFocused: false)
}
super.didUpdateFocus(in: context, with: coordinator)
}
private func isTabbarInHierarchy(view: UIView?) -> Bool {
guard let view = view else {return false}
if view == tabBar {
return true
}
return isTabbarInHierarchy(view: view.superview)
}