解决方案1
我不喜欢它,但它是不依赖于挖掘隐藏子视图(uinavigationbar视图层次结构的内部实现)的最小黑客数量。
首先出现在视图中,我存储“后退”按钮项的备份引用,
然后移除“后退”按钮项(leftbarbuttonem):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
backButtonBackup = self.navigationItem.leftBarButtonItem
self.navigationItem.leftBarButtonItem = nil
}
然后,我恢复back项,但只有在我在viewDidAppear()中调度screen changed事件之后:
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:
禁用导航栏和视图控制器上的所有辅助功能,直到viewDidAppear()完成:
self.navigationController.navigationBar.accessibilityElementsHidden = true
self.view.accessibilityElementsHidden = true
,然后在视图中显示手动将布局元素可访问性事件分派到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...
我也不喜欢这种方法。
在任何情况下,viewdid中的dispatchasync延迟似乎都是必需的——我认为这两种解决方案仍然很糟糕。