SwiftUI Accessibility API

SwiftUI Accessibility API

When the accessibility built into SwiftUI doesn’t provide the right information in the right order, you can use the SwiftUI Accessibility API to make your accessible elements understandable, interactable, and navigable:

Understandable: Provide meaningful labels, values, and other information. Do the accessibility strings provide enough — but not too much — information? To clarify or add context to accessible elements, you can use these modifiers:

  • accessibilityLabel(_:): Override the generated default label.
  • accessibility(addTraits:) or accessibility(removeTraits:): For example, VoiceOver has a rotor feature, and one of the rotor options is Headings. You can speed up navigation for VoiceOver users by adding the isHeader trait to an element like Text so users can quickly navigate between sections of your app’s interface. Note: VoiceOver reads out an element’s traits, so never include them in its label.
  • accessibilityValue(_:): Modify the generated default value so it’s more meaningful to your users.
  • accessibilityHidden(_:): Hide elements that provide unnecessary or redundant information.
  • accessibilityHint(_:): Provide a hint that the user hears only if they seem unsure — describe what happens if the user interacts with the element.

Interactable: Aim to give your accessible elements appropriate default actions, and create custom actions to simplify interaction for users of assistive technology. When your app has custom actions like context menus, double-tap-hold can display them. For example, in Maps with VoiceOver on, users can double-tap-hold an annotation to see the usual context menu.

In VoiceOver, double-tap-hold annotation to show context menu.
In VoiceOver, double-tap-hold annotation to show context menu.

Navigable: You can change the order that VoiceOver visits elements or group elements to reduce the number of steps and speed up navigation for VoiceOver users.

  • accessibilitySortPriority(_:)
  • accessibilityElement(children: .combine)

The accessible UI doesn’t change anything in your app’s visible UI, so you can add more information, in a different order, than what your other users see.

The amount of work for each accessible element could be as little as a few words or lines of code. Or you might need to refactor or add code — or even change a navigation link or alert into a modal sheet.

Most of the time, you’ll add accessibility to your app without changing its appearance and behavior for users who aren’t using VoiceOver. But sometimes, something you do for VoiceOver will inspire an improvement to your visual UI.

Note: There’s one more accessibility attribute: identifier. This is only used in UITests. You’d set an identifier for an element that doesn’t have an accessibility label or if an element’s accessibility label is too long or ambiguous.

The next video shows how to use the SwiftUI Accessibility API to improve the accessibility of a complex app.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo: SwiftUI Accessibility API