我在容器视图中有两个视图,如下所示
@IBOutlet weak var dataDisclosureView: UIStackView! // Main ContainerView
@IBOutlet private weak var titleLabel: UILabel! {
didSet {
titleLabel.text = "Hello"
}
}
@IBOutlet private weak var descriptionLabel: UILabel! {
didSet {
descriptionLabel.text = "World"
}
}
@IBOutlet weak var descriptionView: UIStackView! { // sub container view containing titleLabel and descriptionLabel
didSet {
descriptionView.isAccessibilityElement = true
descriptionView.accessibilityLabel = "Hello"
descriptionView.accessibilityIdentifier = "test_hello"
}
}
@IBOutlet private weak var requestButton: UIButton! {
didSet {
requestButton.isAccessibilityElement = true
requestButton.accessibilityLabel = "Request Button"
requestButton.accessibilityIdentifier = "test_button"
}
}
override func viewDidLoad() {
super.viewDidLoad()
dataDisclosureView.isAccessibilityElement = false
dataDisclosureView.accessibilityElements = [ descriptionView ?? "" ]
if #available(iOS 17.0, *) {
dataDisclosureView.automationElements = [ descriptionView ?? "",
requestButton ?? ""]
} else {
// Fallback on earlier versions
}
let requestButtonAction = UIAccessibilityCustomAction(name: "start",
target: self,
selector: #selector( request))
dataDisclosureView.accessibilityCustomActions = [ requestButtonAction ]
}
我的问题是我想要
descriptionLabel
、titleLabel
、requestButton
和 hintLabel
(用于自动化)的 AccessibilityIdentifers 以及 descriptionView
和 requestButton
(VoiceOver 辅助功能)的辅助功能标签。
但是我无法在 AccessibilityInspector 中看到 Button、TitleLabel 和 descriptionLabel 的
accessibilityIdentifier
。我在这里做错了什么?
PS:我还关注了以下视频:
https://www.youtube.com/watch?v=IAqzXI3kFCk
这是一个可行的解决方案
dataDisclosureView.accessibilityElements = [dataDisclosureView as Any,
titleLabel as Any,
descriptionLabel as Any,
requestButton as Any,
hintLabel as Any]
if #available(iOS 17.0, *) {
dataDisclosureView.automationElements = [titleLabel as Any,
descriptionLabel as Any,
requestButton as Any,
hintLabel as Any]
} else {
// Fallback on earlier versions
}
但我对此并不满意,因为在 Apple WWDC 视频之一中提到,如果我们想要从语音辅助功能中排除项目,请不要将其添加到accessibilityItems中,如果我们想要在自动化元素中添加它,只需添加它即可。他们没有提供更深入的细节。参考是https://www.youtube.com/watch?v=IAqzXI3kFCk还添加了有关我所说内容的屏幕截图
按照这个应该是
dataDisclosureView.accessibilityElements = [dataDisclosureView as Any,
titleLabel as Any,
descriptionLabel as Any,
requestButton as Any]
if #available(iOS 17.0, *) {
dataDisclosureView.automationElements = [titleLabel as Any,
descriptionLabel as Any,
requestButton as Any,
hintLabel as Any]
} else {
// Fallback on earlier versions
}
您通过设置
.isAccessibilityElement = false
使 StackView 对辅助功能不可见
如果您希望子视图可见,请尝试制作
dataDisclosureView.subviews.forEach { $0.isAccessibilityElement = true }
由于您正在执行
dataDisclosureView.accessibilityElements = [ descriptionView ?? "" ]
,因此只有descriptionView对于辅助功能检查器来说是可见的