问题描述
我有一个带有两个按钮的UIView.在MyView
类中,我有以下代码:
I have a UIView with two buttons on it. In the MyView
class I have this code:
-(BOOL) canBecomeFocused {
return YES;
}
-(NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
return @[_editButton, _addButton];
}
-(IBAction) editTapped:(id) sender {
BOOL editing = !tableViewController.editing;
[_editButton setTitle:editing ? @"Done" : @"Edit" forState:UIControlStateNormal];
_addButton.hidden = !editing;
[tableViewController setEditing:editing animated:YES];
}
基本思想是用户可以将焦点移到编辑"按钮,然后可以使添加"按钮出现.
The basic idea is that the user can move the focus to the edit button, which can then make the Add button appear.
问题开始是因为每次我点击编辑"按钮时,焦点都将转移到表格视图上.我实际上希望将其移至添加"按钮.我也想要它,以便在禁用编辑时,编辑"按钮可以保持焦点.但是它又向下移到了表格视图.
The problem started because every time I tapped the edit button, focus would shift to the table view. I would actually like it to move to the Add button. I also want it so that when editing it deactivated, the edit button keeps the focus. but again it's shifting down to the table view.
所以我尝试了上面的代码.这样做的重点在于可以将焦点移到视图和按钮上.但是一旦到达那儿,我就无法将它移动到其他任何地方.
So I tried the above code. This works in that focus can move to the view and on to the button. But once it's there, I cannot get it to move anywhere else.
我读过的所有内容都只是覆盖了preferredFocusEnvironments
,但到目前为止,我还无法使它正常工作.焦点一直指向按钮,然后拒绝移动到其他任何地方.
Everything I've read says just override preferredFocusEnvironments
but so far I've not been able to get this to work. Focus keeps going to a button then refusing to move anywhere else.
有什么想法吗?
推荐答案
如果有人遇到此问题,只需检查是否在控制台中打印了以下调试消息.
If anybody is facing this issue, Just check if you are getting the following debug message printed in the console.
我有以下代码:
// MARK: - Focus Environment
var viewToBeFocused: UIView?
func updateFocus() {
setNeedsFocusUpdate()
updateFocusIfNeeded()
}
override var preferredFocusEnvironments: [UIFocusEnvironment] {
if let viewToBeFocused = self.viewToBeFocused {
self.viewToBeFocused = nil
return [viewToBeFocused]
}
return super.preferredFocusEnvironments
}
我多次调用updateFocus()
方法,而viewToBeFocused
是nil或其他视图.主要在过渡之间调试焦点问题确实很困难.你应该有耐心.
I was calling the updateFocus()
method multiple times while viewToBeFocused
was either nil or some other view. Debugging the focus issues mainly between transition is really difficult. You should have patience.
restoresFocusAfterTransition = false // default is true
如果这是真的,那么即使我们通过调用updateFocusIfNeeded()
强制进行焦点更新,视图控制器也将倾向于聚焦最后一个聚焦的视图.在这种情况下,由于焦点更新已经在进行中,您将在此答案的顶部收到前面提到的警告.
If this is true, the view controller will have the tendancy to focus the last focused view even if we force the focus update by calling updateFocusIfNeeded()
. In this case , since a focus update is already in process, you will get the warning as mentioned before at the top of this answer.
使用以下链接调试焦点问题: https://developer.apple .com/documentation/uikit/focus_interactions/debugging_focus_issues_in_your_app
Use the following link to debug the focus issues: https://developer.apple.com/documentation/uikit/focus_interactions/debugging_focus_issues_in_your_app
首先在编辑方案">启动时传递的参数"下启用焦点调试器:
Enable the focus debugger first under Edit scheme > Arguments passed on launch:
-UIFocusLoggingEnabled YES
这将记录焦点引擎进行的所有更新焦点的尝试.这真的很有帮助.
This will log all the attempts made by the focus engine to update the focus. This is really helpful.
这篇关于tvOS:焦点无法正确移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!