Search code examples
iosswiftaccessibilityvoiceover

iOS VoiceOver reading accessibilityIdentifier instead of accessibilityLabel


VoiceOver should read the accessibilityLabel property of any accessibility element on the screen. I have a UIImageView called mediaImageView, the code below sets up accessibility on this view and is called in awakeFromNib in a UITableViewCell subclass.

Rather than reading image, VoiceOver reads articleMediaCell_image which is the accessibilityIdentifier. Can anyone explain why this this could be happening?

(testing on device with iOS 13.3, issue happens whether or not custom actions are set)

    mediaImageView.isAccessibilityElement = true
    mediaImageView.accessibilityIdentifier = "articleMediaCell_image"
    mediaImageView.accessibilityLabel = "image"

    mediaImageView.accessibilityCustomActions = [
        UIAccessibilityCustomAction(
            name: "expand to fullscreen",
            target: self,
            selector: #selector(imageTapped)
        )
    ]

Solution

  • This happens due to the fact that you have set the trait (image) of the control type as the value for accessibilityLabel here:

    mediaImageView.accessibilityLabel = "image"
    /*
    This won't work, because iOS already knows that this is an image from the element's traits.
    Due to this, the redundant value is ignored and the voiceover falls back to reading the `accessibilityIdentifier`.
    */
    

    Use any value - "media" for example - that is other than the trait of the object, because the voiceover will read the accessibility label value followed by the trait of the control type like so: "Media Image".

    mediaImageView.accessibilityLabel = "media" /* This will work */
    

    Excerpt from accessibilityLabel Apple Documentation:

    Note that the label never includes the control type (such as button) because the traits of the accessibility element contain that information.