问题描述
更新:此错误已在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时,可访问性标签被忽略,只有可见标题由画外音宣布。然而,在iOS 13.7上,可访问性标签被正确使用。UIBarButtonItem用法是否已更改,或者这是iOS错误?
上下文屏幕截图:
推荐答案
当我必须实现UIBarButtonItem
时,我总是遵循these instructions以确保a11y将是稳定的和完全正常的。
我不知道这种情况是错误还是由于新的iOS版本而导致的一种倒退,但在导航栏按钮中实现11y作为定制是一个完美的方式,可以避免遇到任何不幸的意外,即使它看起来像是一个样板解决方案。
我已经创建了一个空白项目,在导航控制器中嵌入了一个简单的视图控制器,其中显示了如下所示的右侧栏按钮:
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.4和Xcode 12.4下验证您的操作。
按照这个原理,您可以使用UIBarButtonItem
来支持iOS 14中的accessibilityLabel属性。