我很难创建一个从UITableView跳转到UIButton的UIFocusGuide。这是调试器上下文截图:
以下是实施:
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 函数。
答案 0 :(得分:2)
将焦点指南添加到按钮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
}