我一直在尝试聚焦第一个按钮,但聚焦引擎将选项卡栏正下方的第三个按钮作为第一个要聚焦的项目。我尝试使用 preferred focus view 但发现当我将按钮按垂直顺序放置时,preferred 会采用首选 View 进行聚焦,但是当我将所有按钮放在水平面上时,它总是采用第三个按钮。我能想到的另一种方法是 Focus Guide,但我想知道在这种情况下它会如何工作?
override weak var preferredFocusedView: UIView? {
get {
return thrirdButton
}
}
发生这种情况是因为焦点引擎将最近的可能焦点元素作为第一个元素,正如您从附图中看到的那样。我附上了 View Controller 的上下文屏幕截图。任何解决此问题的帮助或线索将不胜感激。
最佳答案
解决方案: 我们需要在按钮 3 的正上方添加焦点指南,并在焦点指南获得焦点时将其重定向到按钮 1。
private var focusGuide = UIFocusGuide()
self.view.addLayoutGuide(focusGuide)
self.focusGuide.bottomAnchor.constraintEqualToAnchor(self.button3.topAnchor).active = true
self.focusGuide.leftAnchor.constraintEqualToAnchor(self.button3.leftAnchor).active = true
// Width and height
self.focusGuide.widthAnchor.constraintEqualToAnchor(self.button3.widthAnchor).active = true
self.focusGuide.heightAnchor.constraintEqualToAnchor(self.button3.heightAnchor).active = true
focusGuide.centerXAnchor.constraintEqualToAnchor(self.button3.centerXAnchor).active = true
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
self.focusGuide.preferredFocusedView = button1
}
关于objective-c - tvOS:当按钮处于水平顺序时,将第一个按钮作为首选焦点 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34966487/