我想将辅助功能焦点设置为导航栏的标题项。
默认情况下,焦点从左上角设置,这意味着后退按钮将处于焦点上。
我希望标题项能够成为焦点。
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification,
navigationController?.navigationBar.items?.last)
答案 0 :(得分:1)
解决方案1 我不喜欢它,但这是最少的黑客攻击,它不依赖于挖掘隐藏的子视图(UINavigationBar视图层次结构的内部实现)。
首先在viewWillAppear中,存储后退按钮项的备份参考, 然后删除后退按钮项(leftBarButtonItem):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
backButtonBackup = self.navigationItem.leftBarButtonItem
self.navigationItem.leftBarButtonItem = nil
}
然后,我还原后退项目,但只有在我在viewDidAppear()中调度屏幕更改事件之后,才可以进行还原:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
self?.navigationItem.leftBarButtonItem = self?.backButtonBackup
}
}
解决方案2: 禁用导航栏上的所有可访问性,并关闭view controller,直到viewDidAppear()完成:
self.navigationController.navigationBar.accessibilityElementsHidden = true
self.view.accessibilityElementsHidden = true
,然后在viewDidAppear中手动将布局元素可访问性焦点事件调度到UINavigationBar的标签子视图:
UIAccessibilityPostNotification( UIAccessibilityLayoutChangedNotification, self.navigationController.navigationBar.subviews[2].subviews[1])
// The label buried inside the nav bar. Not tested on all iOS versions.
// Alternately you can go digging for the label by checking class types.
// Then use DispatchAsync, to re-enable accessibility on the view and nav bar again...
我也不喜欢这种方法。
在任何情况下,似乎都需要在viewDidAppear中添加DispatchAsync延迟-我认为这两种解决方案仍然令人恐惧。
答案 1 :(得分:0)
首先,我们需要创建一个有用的扩展程序:
extension UIViewController {
func setAccessibilityFocus(in view: UIView) {
UIAccessibility.post(notification: .screenChanged, argument: view)
}
}
然后,我们将可以在导航栏标题中设置焦点,如下所示:
setAccessibilityFocus(in: self.navigationController!.navigationBar.subviews[2].subviews[1])