I have two views in a container view as below
@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 ]
}
My issue is I want AccessibilityIdentifers for descriptionLabel
, titleLabel
, requestButton
and hintLabel
(For Automation) and accessibility labels for descriptionView
and requestButton
(VoiceOver Accessibility).
But I am unable to see accessibilityIdentifier
for Button, TitleLabel and descriptionLabel in AccessibilityInspector. what am I doing wrong here?
PS: I also followed below video :
https://www.youtube.com/watch?v=IAqzXI3kFCk
This is a working solution
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
}
but I am not satisfied with that because in one of Apple WWDC video it was mentioned that if we want exempt item from voice over accessibility don’t add it in accessibilityItems and if we want in automation elements just add it. They haven’t provided deeper details . Reference is https://www.youtube.com/watch?v=IAqzXI3kFCk also adding screenshot about what I am saying
According to this it should be
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
}
1
You’ve made dataDisclosureView invisible to VoiceOver by setting isAccessibilityElement = false. When isAccessibilityElement is set to false on a container view, it prevents the container itself from being read by VoiceOver. However, this can also limit the visibility of its subviews in Accessibility Inspector, depending on how accessibilityElements is configured.
Ensure Subviews Are Individually Accessible:
To make each subview accessible, you can iterate through them and set isAccessibilityElement = true:
dataDisclosureView.subviews.forEach { $0.isAccessibilityElement = true }
This will allow titleLabel, descriptionLabel, and requestButton to be individually accessible and show up in Accessibility Inspector.
Configure accessibilityElements for Custom Order:
Since you’ve set dataDisclosureView.accessibilityElements = [descriptionView ?? “”], only descriptionView is currently accessible to VoiceOver. If you need additional elements to be read (like titleLabel, descriptionLabel, and requestButton), include them in the accessibilityElements array in the order you want them read:
dataDisclosureView.accessibilityElements = [descriptionView as Any, requestButton as Any]