通过UIFocusGuide从UITableView滑动到UIButton无法正常工作

时间:2017-02-22 19:23:32

标签: tvos

我无法向右滑动以将焦点从UITableView移动到桌面视图右侧和下方的UIButton。我已经设置了UIFocusGuide,我相信我的几何形状是正确的。 (参见附件截图,使用Pod VisualFocusGuide创建。)

当我在UIFocusUpdateContext上使用内置的QuickLook时,它只显示表视图焦点(一种颜色的突出显示的行,另一种颜色的其他行),但这可能是因为我只能获得焦点在表视图中,所以也许上下文仅限于表。

我看过一个建议elsewhere,将焦点指南与按钮相关联,而不是控制器的视图,但这不起作用。出于绝望,我还尝试将焦点指南与tableview相关联,但仍然没有运气。

以下是相关方法。任何人都可以放下任何光线将不胜感激!谢谢!

    override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addLayoutGuide(focusGuide)

    self.focusGuide.widthAnchor.constraint(equalTo: self.infoButton.widthAnchor).isActive = true
    self.focusGuide.heightAnchor.constraint(equalTo: self.tableView.heightAnchor).isActive = true
    self.focusGuide.topAnchor.constraint(equalTo: self.tableView.topAnchor).isActive = true
    self.focusGuide.leftAnchor.constraint(equalTo: self.infoButton.leftAnchor).isActive = true

    self.focusGuide.preferredFocusEnvironments = [self.infoButton]
}

    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocus(in: context, with: coordinator)

    guard let nextFocusedView = context.nextFocusedView else { return }

    // When the focus engine focuses on the focus guide, we can programmatically tell it which element should be focused next.
    switch nextFocusedView {
    case self.tableView:
        self.focusGuide.preferredFocusEnvironments = [self.infoButton]

    case self.infoButton:
        self.focusGuide.preferredFocusEnvironments = [self.tableView]

    default:
        self.focusGuide.preferredFocusEnvironments = []
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我刚刚开始工作了。我在表格视图下方的按钮左侧添加了另一个焦点指南。我把它的大小设置为tableview的宽度和按钮的高度。我将preferredFocusEnvironment作为tableView。但这本身还不够。然后我删除了 didUpdateFocus方法,左右滑动开始工作!这是结果代码:

    override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addLayoutGuide(focusGuide1)
    self.focusGuide1.topAnchor.constraint(equalTo: self.tableView.topAnchor).isActive = true
    self.focusGuide1.leftAnchor.constraint(equalTo: self.infoButton.leftAnchor).isActive = true
    self.focusGuide1.heightAnchor.constraint(equalTo: self.tableView.heightAnchor).isActive = true
    self.focusGuide1.widthAnchor.constraint(equalTo: self.infoButton.widthAnchor).isActive = true
    self.focusGuide1.preferredFocusEnvironments = [self.infoButton]

    self.view.addLayoutGuide(focusGuide2)
    self.focusGuide2.topAnchor.constraint(equalTo: self.infoButton.topAnchor).isActive = true
    self.focusGuide2.leftAnchor.constraint(equalTo: self.tableView.leftAnchor).isActive = true
    self.focusGuide2.heightAnchor.constraint(equalTo: self.infoButton.heightAnchor).isActive = true
    self.focusGuide2.widthAnchor.constraint(equalTo: self.tableView.widthAnchor).isActive = true
    self.focusGuide2.preferredFocusEnvironments = [self.tableView]
}