本文介绍了如何强制焦点更改为 tvOS 中的特定视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在实施自定义代码来处理对 Siri Remote 上的菜单按钮的点击.按下菜单按钮时,如何强制焦点切换到我的自定义菜单?
I am implementing custom code to handle a click on the Menu button on the Siri Remote.How can I force focus to change to my custom menu when pressing the menu button?
推荐答案
我自己终于想通了.您必须覆盖 UIView
或 UIViewController
的 preferredFocusedView
属性.
Finally figured it out myself. You have to override the preferredFocusedView
property of your UIView
or UIViewController
.
在 Swift 中它是这样工作的:
In Swift it works like this:
func myClickHandler() {
someCondition = true
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
someCondition = false
}
override weak var preferredFocusedView: UIView? {
if someCondition {
return theViewYouWant
} else {
return defaultView
}
}
我不太记得如何在 Objective-C 中覆盖 getter,所以如果有人想发帖,我会编辑答案.
I can't quite remember how to override getters in Objective-C so if someone want to post that I'll edit the answer.
这篇关于如何强制焦点更改为 tvOS 中的特定视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!