Search code examples
iosswiftuikituiaccessibility

Does VoiceOver always ignore views with width and height zero iOS?


For Voice Over to ignore a UILabel is it enough to set its text property to nil or is it also necessary to set the isHidden property to true as well ?

I tested this in a sample project and saw that Voice Over ignores the label when the text is set to nil. Can I always count on this? In this case the label would have a width and height of zero since I did not add any constraints. So can I assume that any view with a width and height of zero would be ignored by Voice Over irrespective of whether it is hidden or not?


Solution

  • In order for it [UILabel] to be ignored by VoiceOver is setting text to nil enough or should I also set the isHidden property to true as well?

    The appropriate way for an element to be ignored by VoiceOver is set its isAccessibilityElement property to false👍... among other ways that depend on what elements you're working with ⟹ for a UILabel it's enough.
    Setting its text to nil works in this case but it's more a kludge than a proper solution.

    Graphic elements shouldn't disappear only when VoiceOver is running because your interface should be the same for everyone: for instance, you may have unlettered persons who could use your app with or without VoiceOver and they might be confused if they haven't the same visual experience in both ways. 😨

    I tested this in a sample project and saw that Voice Over ignores the label when the text is set to nil. Can I always count on this?

    No, you can't because it works for a UILabel but if you try and do the same action with a button or a text field, you'll notice that it won't work certainly because of their 'accessibilityTraits`.

    So can I assume that any view with a width and height of zero would be ignored by Voice Over irrespective of whether it is hidden or not?

    Yes, you can because {width = height = zero} means that this element isn't a graphic element that VoiceOver should analyze and read out.
    But, even if it works, I don't recommend to follow this path to hide elements to VoiceOver as I previously explained. 😉