UIFocusGuide UITableView和UIButton

我很难创build一个将从UITableView跳转到UIButton的UIFocusGuide。 这里是debugging器上下文截图:

在这里输入图像说明

这是执行:

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函数永远不会被调用。

将焦点指南添加到button而不是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 }