[英]Accessibility set focus to navigation bar title item
I would like to set the accessibility focus to the navigation bar's title item.我想将可访问性焦点设置为导航栏的标题项。
By default the focus is set from top left, meaning the back button would be on focus.默认情况下,焦点是从左上角设置的,这意味着后退按钮将处于焦点上。
I would like the title item to be in focus.我希望标题项目成为焦点。
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification,
navigationController?.navigationBar.items?.last)
Solution 1 I don't like it, but it was the minimum amount of hacking that does not rely on digging through hidden subviews (internal implementation of UINavigationBar view hierarchy).解决方案 1我不喜欢它,但它是不依赖于挖掘隐藏子视图(UINavigationBar 视图层次结构的内部实现)的最小数量的黑客攻击。
First in viewWillAppear, I store a backup reference of the back button item, and then remove the back button item (leftBarButtonItem):首先在viewWillAppear中,我存储了后退按钮项的备份引用,然后移除后退按钮项(leftBarButtonItem):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
backButtonBackup = self.navigationItem.leftBarButtonItem
self.navigationItem.leftBarButtonItem = nil
}
Then I restore the back item, but only after I dispatch the screen changed event in viewDidAppear() :然后我恢复了后面的项目,但只有在我在 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
}
}
Solution 2: Disable all accessibility on the nav bar and view controller up until viewDidAppear() is finished:解决方案 2:禁用导航栏和视图控制器上的所有辅助功能,直到 viewDidAppear() 完成:
self.navigationController.navigationBar.accessibilityElementsHidden = true
self.view.accessibilityElementsHidden = true
, and then in viewDidAppear manually dispatching the layout element accessibility focused event to the label subview of UINavigationBar: ,然后在 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...
I'm not a fan of this method either.我也不喜欢这种方法。
DispatchAsync delay in viewDidAppear seems to be needed in any case - and I think both solutions are still horrible.在任何情况下似乎都需要 viewDidAppear 中的 DispatchAsync 延迟 - 我认为这两种解决方案仍然很糟糕。
First, we'll need to create an useful extension:首先,我们需要创建一个有用的扩展:
extension UIViewController {
func setAccessibilityFocus(in view: UIView) {
UIAccessibility.post(notification: .screenChanged, argument: view)
}
}
Then, we'll be able to set our focus in the navigation bar title like this:然后,我们将能够像这样在导航栏标题中设置焦点:
setAccessibilityFocus(in: self.navigationController!.navigationBar.subviews[2].subviews[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.