【问题标题】:Focus problem on tvOS app when moving from a view to another从一个视图移动到另一个视图时关注 tvOS 应用程序的问题
【发布时间】:2019-10-01 10:41:16
【问题描述】:

喂,

我有一个关于 tvOS 的视图,其中包含两个不同大小的视图。当我从左视图移动时,我想从左视图聚焦项目,但这没有发生。

这是它的外观:View

左视图是一个tableView右视图是一个UIView,它有一个顶部的文本字段。当我从 tableview 移动到专注于 textField 时,我想要。但现在它没有发生。

我在 ma​​inController 上创建了一个 focusGuide,其中包含与 ma​​inController 具有相同锚点的两个视图,但它不起作用。像这样:

 private func setupFocusGuide(){
    self.sideFocusGuide = UIFocusGuide()
    view.addLayoutGuide(self.sideFocusGuide!)
    self.sideFocusGuide?.topAnchor.constraint(equalTo: self.mainView.topAnchor, constant: 0).isActive = true
    self.sideFocusGuide?.bottomAnchor.constraint(equalTo: self.mainView.bottomAnchor, constant: 0).isActive = true
    self.sideFocusGuide?.leftAnchor.constraint(equalTo: self.mainView.leftAnchor).isActive = true
    self.sideFocusGuide?.rightAnchor.constraint(equalTo: self.mainView.rightAnchor).isActive = true
    self.sideFocusGuide?.preferredFocusEnvironments = [self.rightView]
}

【问题讨论】:

    标签: swift tvos


    【解决方案1】:

    我认为您应该使用另一个 UIFocusGuideUITableViewUITextField 之间切换焦点:

    private var focusGuideTextFieldAndTableView: UIFocusGuide!
    

    初始化它:

    focusGuideTextFieldAndTableView = UIFocusGuide()
    view.addLayoutGuide(focusGuideTextFieldAndTableView)
    focusGuideTextFieldAndTableView.leftAnchor.constraint(equalTo: tableView.rightAnchor).isActive = true
    focusGuideTextFieldAndTableView.topAnchor.constraint(equalTo: tableView.topAnchor).isActive = true
    focusGuideTextFieldAndTableView.bottomAnchor.constraint(equalTo: tableView.bottomAnchor).isActive = true
    focusGuideTextFieldAndTableView.rightAnchor.constraint(equalTo: textField.leftAnchor).isActive = true
    focusGuideTextFieldAndTableView.preferredFocusEnvironments = [textField]
    

    didUpdateFocus(in:with:) 中切换preferredFocusEnvironments

    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        super.didUpdateFocus(in: context, with: coordinator)
        if context.nextFocusedView == textField {
            focusGuideTextFieldAndTableView.preferredFocusEnvironments = [tableView]
        } else if context.previouslyFocusedView == textField {
            focusGuideTextFieldAndTableView.preferredFocusEnvironments = [textField]
        }
    }
    

    【讨论】:

    • 这比简单地设置preferredFocusEnvironments 变量要好得多。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多