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

查看:310
本文介绍了在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
    }
}

你可以n也很有趣并且变换框架来模仿默认的焦点效果!

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

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

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