我正在将可访问性实现到我们当前的项目中,并且在将焦点设置在以前的视图控制器中的元素上时遇到了一些问题。
一个例子是:
用户在索引 3 处的表视图单元格上进行选择。 在didSelectRowAt
中,我们向用户呈现一个用户进行选择的UIActionSheet
。 一旦用户做出选择并且显示的UIActionSheet
被关闭,应在 tableview 单元格索引 3 中选择辅助功能焦点,而是将焦点设置回视图中的初始元素,在我的例子中是左上角的元素。
我在呈现模态视图时使用了 UIAccessibility.Notification
.screenChanged
和 UIView.accessibilityViewIsModal
来设置新的焦点,但这似乎没有一个好的解决方案可以在关闭视图时指向以前的元素。
有关如何跟踪以前的辅助功能元素焦点的任何见解将不胜感激。
首先,创建一个变量,该变量将存储TableViewController
中的先前位置:
var previousPosition: IndexPath?
然后,在点击单元格后存储此位置:
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
previousPosition = indexPath
}
最后,当显示的UIActionSheet
被关闭时(下面的后退按钮示例(,以下代码片段选择并聚焦已触发最新点击的表视图单元格:
var actionSheet: UIAlertController?
actionSheet = UIAlertController(title: "Action Sheet",
message: "Ready to go back?",
preferredStyle: .actionSheet)
let backButton = UIAlertAction(title: "B.A.C.K.",
style: .cancel,
handler: { (action) -> Void in
print("Back button tapped")
self.tableView.selectRow(at: self.previousPosition,
animated: true,
scrollPosition: .none)
let currentCell = self.tableView.cellForRow(at: self.previousPosition!)
UIAccessibility.post(notification:.layoutChanged,
argument: currentCell)
})
actionSheet!.addAction(backButton)
在空白项目中查看此代码片段的结果,并在应用中对其进行调整,使其按预期工作。
编辑:除了我的代码片段之外,请查看表视图的 remembersLastFocusedIndexPath
实例属性,该属性可能与视图控制器的restoresFocusAfterTransition
实例属性一起提供直接结果。