所以我有一些按钮是通过 Interface Builder 添加到 View 中的,而不是使用我自定义的系统按钮。我试图弄清楚如何在不同状态(突出显示、聚焦等)期间更改特性,例如文本颜色和背景颜色。
我似乎无法通过 IB 完成此操作,因此我可能会创建 UIButton 的子类并在那里更改它们,但我无法找到要更改的属性。我没有看到文档中明确提到它们
最佳答案
@Yoseob Lee是正确的,但他的回答缺少一些细节。这是一个稍微更好(主观)的答案。
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if context.nextFocusedView === self {
coordinator.addCoordinatedAnimations({
// change the appearance as desired
}, completion: nil)
} else if context.previouslyFocusedView === self {
coordinator.addCoordinatedAnimations({
// revert back to default appearance
}, completion: nil)
}
}
注意 ===
而不是 ==
进行比较。在大多数情况下,比较引用更快。此外,将任何外观更改添加到动画协调器组更改为默认动画(和时间),以便外观更改看起来更像是 tvOS 默认行为的一部分。此外,确保只有 previouslyFocusedView
恢复到默认状态可以减少不必要的操作。
关于swift - 在 Swift (tvOS) 中,如何更改 UIButton 的突出显示和焦点颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643387/