繁体   English   中英

UIFocusGuide UITableView和UIButton

[英]UIFocusGuide UITableView and UIButton

我很难创建一个UIFocusGuide,它将从UITableView跳转到UIButton。 这是调试器上下文屏幕截图:

在此处输入图片说明

这是实现:

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
        }

    }

当我位于UITableView的中间项或UITableView的末尾时,永远不会调用didUpdateFocusInContext函数。

将聚焦指南添加到按钮而非self.view 您不需要覆盖didUpdateFocusInContext例如:

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM