问题描述
更新:此错误已在 iOS 14.5 中修复
我在 UINavigationController
中嵌入了以下类:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let barButton = UIBarButtonItem(title: "Save",style: .plain,target: nil,action: nil)
barButton.accessibilityLabel = "Save meeting"
navigationItem.rightBarButtonItem = barButton
}
}
在运行 iOS 14.4 时,无障碍标签会被忽略,VoiceOver 仅会播报可见标题。但是,在 iOS 13.7 上,可正确使用辅助功能标签。 UIBarButtonItem 的用法是否发生了变化,还是 iOS 错误?
上下文截图:
解决方法
当我必须实现 ranking.json
时,我总是遵循 these instructions 以确保 a11y 稳定且功能齐全。 ?
我不知道这种情况是错误还是由于新的 iOS 版本而导致的回归,但是在导航栏按钮中实现 a11y 作为自定义是一种完美的方式,即使它看起来像样板解决方案。 ?
我创建了一个空白项目,在导航控制器中嵌入了一个简单的视图控制器,其中显示的右侧栏按钮如下所示:
UIBarButtonItem
在 iOS 14.4 和 Xcode 12.4 下,您的右侧栏按钮显示 “OK”,VoiceOver 读出“验证您的操作”。 ?
根据此原理,您可以使用 class NavBarViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
var a11yRightBarButton: UIBarButtonItem?
let a11y = UILabel()
a11y.text = "OK"
a11y.sizeToFit()
a11y.isUserInteractionEnabled = true //Mandatory to use the 'tap gesture'.
a11yRightBarButton = UIBarButtonItem(customView: a11y)
let tap = UITapGestureRecognizer(target: self,action: #selector(validateActions(info:)))
a11yRightBarButton?.customView?.addGestureRecognizer(tap)
a11yRightBarButton?.isAccessibilityElement = true
a11yRightBarButton?.accessibilityTraits = .button
a11yRightBarButton?.accessibilityLabel = "validate your actions"
navigationItem.rightBarButtonItem = a11yRightBarButton
}
@objc func validateActions(info: Bool) -> Bool {
print("hello")
return true
}
}
来支持 iOS 14 中的 accessibilityLabel 属性。 ?
将 UIBarButtonItem 的可访问性设置为 true。
let barButton = UIBarButtonItem(title: "Save",style: .plain,target: nil,action: nil)
barButton.accessibilityLabel = "Save meeting"
barButton.isAccessibilityElement = true
navigationItem.rightBarButtonItem = barButton