UIFocusGuide UITableView 和 UIButton [英] UIFocusGuide UITableView and UIButton

查看:44
本文介绍了UIFocusGuide UITableView 和 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难创建从 UITableView 跳转到 UIButton 的 UIFocusGuide.这是调试器上下文屏幕截图:

I am having a hard time creating a UIFocusGuide that will jump from the UITableView to a UIButton. Here is the debugger context screenshot:

这是实现:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self

        // add the focus guide
        self.view.addLayoutGuide(focusGuide)

        // add the anchors
        self.focusGuide.leftAnchor.constraintEqualToAnchor(self.button.leftAnchor).active = true
        self.focusGuide.topAnchor.constraintEqualToAnchor(self.tableView.topAnchor).active = true
        self.focusGuide.widthAnchor.constraintEqualToAnchor(self.button.widthAnchor).active = true
        self.focusGuide.heightAnchor.constraintEqualToAnchor(self.tableView.heightAnchor).active = true
    }

    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

        guard let nextFocusedView = context.nextFocusedView else { return }

        switch nextFocusedView {
        case self.button:
            self.focusGuide.preferredFocusedView = self.button

        case self.tableView:
            self.focusGuide.preferredFocusedView = self.tableView

        default:
            self.focusGuide.preferredFocusedView = nil
        }

    }

didUpdateFocusInContext 函数永远不会在我处于 UITableView 的中间项或 UITableView 的末尾时被调用.

The didUpdateFocusInContext function is never getting called when I am at the middle item of the UITableView or the end of the UITableView.

推荐答案

将焦点引导添加到按钮而不是 self.view.您不需要覆盖 didUpdateFocusInContext 例如:

Add the focus guide to the button not self.view. You don't need override didUpdateFocusInContext For example:

var focusGuide = UIFocusGuide()
override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self

        // add the focus guide
        self.button.addLayoutGuide(focusGuide)

        // add the anchors
        self.focusGuide.leftAnchor.constraintEqualToAnchor(self.button.leftAnchor).active = true
        self.focusGuide.topAnchor.constraintEqualToAnchor(self.tableView.topAnchor).active = true
        self.focusGuide.widthAnchor.constraintEqualToAnchor(self.button.widthAnchor).active = true
        self.focusGuide.heightAnchor.constraintEqualToAnchor(self.tableView.heightAnchor).active = true
}

这篇关于UIFocusGuide UITableView 和 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆