objective-c - tvOS:当按钮处于水平顺序时,将第一个按钮作为首选焦点 View

标签 objective-c swift2 tvos apple-tv

我一直在尝试聚焦第一个按钮,但聚焦引擎将选项卡栏正下方的第三个按钮作为第一个要聚焦的项目。我尝试使用 preferred focus view 但发现当我将按钮按垂直顺序放置时,preferred 会采用首选 View 进行聚焦,但是当我将所有按钮放在水平面上时,它总是采用第三个按钮。我能想到的另一种方法是 Focus Guide,但我想知道在这种情况下它会如何工作?

 override weak var preferredFocusedView: UIView? {
        get {
            return thrirdButton
        }
    }

发生这种情况是因为焦点引擎将最近的可能焦点元素作为第一个元素,正如您从附图中看到的那样。我附上了 View Controller 的上下文屏幕截图。任何解决此问题的帮助或线索将不胜感激。

Focus path expected-Blue vs default-Red Context Path

最佳答案

解决方案: 我们需要在按钮 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
    }

Context for the focus guide shown in sky blue

关于objective-c - tvOS:当按钮处于水平顺序时,将第一个按钮作为首选焦点 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34966487/

相关文章:

iOS 根据条件为 UIView 交换类

objective-c - 我可以在 MKMapView 中更改 Google Logo 的位置吗?

ios - NSString componentsSeparatedByString "&"但忽略 "& amp;"

xcode - 更改 ParentViewController 中 ChildViewController 中 UIPageControl 的点 - swift

ios - iOS 上的深度页面转换

ios - 如何从 Swift 中的 .m3u8 格式视频 url 获取缩略图?

ios - 在其他类获取数据时停止 viewDidLoad 的其余部分

swift - 在 Swift 中创建随机 Bool

app-store-connect - 从 iTunesconnect 移除 tvOS 构建

ios - 以编程方式在 Xcode 中获取设备纵横比