uikitvoiceover

Can a UIView object combine the accessibility properties of its children automatically?


I have a view hierarchy as follows:

UIView
  \-- UIStackView
    +-- UILabel
    \-- UILabel

By default, VoiceOver skips over the UIView and stops on each of the UILabels separately.

I would like VoiceOver to stop on the UIView and combine its contents (i.e. read the accessibility label of each of its accessible children one by one). Does UIKit offer an API to achieve this similar to SwiftUI's accessibilityElement(children: .combine) API?

Notes

I know I can set isAccessibilityElement to true on the UIView and then set its accessibilityLabel to "\(label1.text!) \(label2.text!)". I'm wondering whether there is an automatic means of achieving this which doesn't require me to define and maintain the UIView's accessibilityLabel.


Solution

  • UIKit does not offer an API like SwiftUI's accessibilityElement(children: .combine) for merging the accessibility labels of all non-hidden children automatically into the parent. I have submitted a suggestion for this in Feedback Assistant though I am doubtful it will ever see the light of day.

    As suggested by an Apple Frameworks Engineer in the Apple Developer Forums here, a workaround is to define the parent view's accessibility label by means of the stack view that sits between the parent and its children, as follows:

    parentView.accessibilityLabel = stackView.arrangedSubviews
        .map { $0.accessibilityLabel ?? "" }
        .joined(separator: " ")