在 Swift (tvOS) 中,如何更改 UIButton 的突出显示和焦点颜色? [英] In Swift (tvOS) how do you change a UIButton's highlight and focus colors?

查看:21
本文介绍了在 Swift (tvOS) 中,如何更改 UIButton 的突出显示和焦点颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我通过 Interface Builder 添加了一些按钮,而不是使用自定义的系统按钮.我试图弄清楚如何更改特征,例如在不同状态(突出显示、聚焦等)期间的文本颜色和背景颜色.

So I have some buttons that I've added to a view through the Interface Builder and rather than using the system buttons I made them custom. I'm trying to figure out how to change characteristics, such as the text color and background color during different states (highlighted, focused, etc).

我似乎无法通过 IB 来实现,所以我可能会创建 UIButton 的子类并在那里更改它们,但我无法找到要更改的属性.我没有看到文档中明确提到它们

It doesn't seem like I can do it through the IB so I'll probably make a subclass of UIButton and change them there, but I'm having trouble finding what properties to change. I don't see them explicitly mentioned in the documentation

推荐答案

你绝对是在正确的轨道上!

You're definitely on the right track!

一旦你继承了 UIButton,你就可以重写函数 didUpdateFocusInContext(来自 UIFocusEnvironment 协议,tvOS 上的 UIButton 已经实现了)

Once you subclass UIButton, you can override the function didUpdateFocusInContext (from UIFocusEnvironment protocol, which UIButton on tvOS already implements)

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

    if context.nextFocusedView == self {
        // This is when the button will be focused
        // You can change the backgroundColor and textColor here
    } else {
        // This is when the focus has left and goes back to default
        // Don't forget to reset the values
    }
}

您还可以花哨并变换框架以模仿默认的焦点"效果!

You can also get fancy and transform the frame to imitate the default "focus" effect!

这篇关于在 Swift (tvOS) 中,如何更改 UIButton 的突出显示和焦点颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆