问题描述
我想要做的就是为所有状态的按钮添加背景颜色.但是我想保留在 tvOS
故事板中使用系统按钮时免费"获得的自动焦点阴影.到目前为止,我还没有找到允许这样做的组合.
All I want to do is add a background color to a button for all states. But I want to maintain the automatic focus shadow that you get "for free" when using a system button in the tvOS
storyboard. So far I haven't been able to find a combination that allows this.
或者,我也对一种在按钮聚焦时以编程方式添加阴影的方法感兴趣,但是缺少对按钮的子类化(我还没有尝试过),我也不知道该怎么做.
Alternatively, I would also be interested in a way to programmatically add the shadow when the button is focused, but short of subclassing the button (which I haven't yet tried), I don't know how to do that either.
推荐答案
覆盖 didUpdateFocusInContext
方法并检查下一个焦点视图是否是按钮,如果是则自定义其 UI,并将其设置回原始状态检查 context.previousFocusedView
是那个按钮,如下所示
Override didUpdateFocusInContext
method and check if next focus view is button, if yes then customize its UI, and to set it back to orignal state check context.previousFocusedView
was that button, something like below
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
if (context.nextFocusedView == _button)
{
// set background color
}
else if (context.previousFocusedView == _button)
{
// set background color to background
}
}
这篇关于如何在仍显示焦点的同时为 tvOS 创建具有背景颜色的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!