skip to content

VoiceOver and Dynamic Type

SwiftUI views get accessibility from their content, refined with labels, traits, grouping and custom actions, and adapt with Dynamic Type. Interviewers ask how to make a control VoiceOver-ready.

on this pageshow

explore

questions

6

In SwiftUI, how do you make an icon-only play button readable by VoiceOver, and what does SwiftUI already infer from Text, Image and Button?

level: juniorimportance: must knowfreq 78%

answer

  1. the title can stay, just hidden
  2. labelStyle(.iconOnly) keeps the name
  3. accessibilityLabel names, not describes
  4. Image(decorative:) for ornaments

basics

~20 s

SwiftUI reads a Text's string, gives a Button its label content plus the button trait, and skips Image(decorative:). An icon-only button needs a spoken name: give it a title and hide it with .labelStyle(.iconOnly), or add .accessibilityLabel("Play").

solid answer

~40 s

SwiftUI builds the accessibility tree from the views themselves: a `Text` is read as its string, a `Button` becomes one element named by its label content with the `.isButton` trait, and an `Image(decorative:)` is skipped. The trouble is an icon-only button, because a symbol image gives VoiceOver nothing that says what the button *does*. The cleanest fix is to keep a real title and only hide it visually: `Button("Play", systemImage: "play.fill", action: play)` with `.labelStyle(.iconOnly)`, which Apple's Button documentation says still uses the title in VoiceOver. When the label must be a bare `Image`, add `.accessibilityLabel("Play")`. Keep the label short and never append "button": the trait already says that. Use `accessibilityValue` for state that changes and `accessibilityHint` only for a non-obvious result.

code

swift · 24 lines
swift
import SwiftUI

struct TransportControls: View {
    @Binding var isPlaying: Bool
    @Binding var isFavorite: Bool

    var body: some View {
        HStack(spacing: 32) {
            Button("Previous track", systemImage: "backward.fill") {}
            Button(isPlaying ? "Pause" : "Play",
                   systemImage: isPlaying ? "pause.fill" : "play.fill") {
                isPlaying.toggle()
            }
            Button("Next track", systemImage: "forward.fill") {}

            Button { isFavorite.toggle() } label: {
                Image(systemName: isFavorite ? "heart.fill" : "heart")
            }
            .accessibilityLabel("Favorite")
            .accessibilityAddTraits(isFavorite ? .isSelected : [])
        }
        .labelStyle(.iconOnly)  // icons drawn; VoiceOver still reads the titles
    }
}

go deeper

for a junior

Recall the three tools: a titled Button with .labelStyle(.iconOnly), accessibilityLabel for a bare image, and Image(decorative:) for ornaments. Say why the label never contains the word button.

for a middle

Explain what SwiftUI infers on its own from Text, Button and labeled images, and how label, value, hint and traits divide the job of describing one control.

for a senior

Show how you would keep labels correct across a codebase: titled buttons as the house pattern, a constant label with state in traits or value, and a VoiceOver pass on a device for every new icon.

for a principal

Weigh making titled, icon-only buttons the design-system default against case-by-case labels, since the default removes a whole class of missing-name defects from review.

## What SwiftUI gives you for free SwiftUI has accessibility built in. Apple's documentation says the framework introspects common elements such as lists, text fields, sliders and **buttons** and provides basic accessibility labels and values by default. In practice that means: - A **`Text`** becomes an element whose label is its string. - A **`Button`** becomes one element whose label comes from its label content, with the **`.isButton`** trait, so VoiceOver says "Play, button". - A **labeled image** (`Image(_:bundle:)`, or the initializers that take a `label:` argument) is meant to be content for controls, and the label names it. - A **decorative image** (`Image(decorative:)`) is unlabeled, and accessibility systems ignore it. - Standard controls such as `Toggle` and `Slider` report their own value and adjust themselves. **VoiceOver** is the screen reader built into iOS: it moves focus from element to element and speaks each one's label, value, traits and hint. A view that has no good label leaves the person guessing. ## Why icon-only buttons break A transport bar with play, previous and next buttons drawn as SF Symbols has no visible text, so there is no string for SwiftUI to use. At best VoiceOver gets a name derived from the artwork, which describes the picture rather than the action, and an unlabeled custom image gives it even less. It is one of the most common accessibility defects in SwiftUI code review, and one of the cheapest to prevent. ## The two fixes 1. **Keep a title and hide it visually.** Use `Button("Play", systemImage: "play.fill", action: play)`, or a `Label` inside the button, and apply `.labelStyle(.iconOnly)`. Apple's Button documentation says this style shows only the icon "but still use[s] its title to describe the button in accessibility modes like VoiceOver". The title is also extracted for localization and shown in menus and toolbars that draw titles. 2. **Name a bare image explicitly.** When the label must be a custom `Image`, add `.accessibilityLabel("Play")` to the button. Prefer the first: the name lives in the control itself, so VoiceOver, menus and toolbars all use the same string. Apple's guidance is to avoid labels that use only images without an accessibility label. ## Label, value, hint and traits Each modifier answers a different question, and mixing them up is what makes VoiceOver output sound wrong. | Modifier | Answers | Example | |---|---|---| | `accessibilityLabel` | What is it? | "Volume", "Play" | | `accessibilityValue` | What is its current state? | "60%" | | `accessibilityHint` | What happens if I activate it? | "Purchases the item" | | `accessibilityAddTraits` / `accessibilityRemoveTraits` | What kind of thing is it? | `.isHeader`, `.isSelected` | Rules that follow from Apple's documentation: - **Never repeat the role in the label.** Apple's example: don't use "Play button", because the button trait already identifies it. - **Use a value only when it differs from the label**, such as the current volume of a slider labelled "Volume". - **Keep hints short and optional.** A hint is a brief phrase about the result, spoken after a pause, and the label must stand without it. - **Express state through traits or value**, not by renaming the control. A favorite button keeps the label "Favorite" and adds `.isSelected` when it is on. - **Mark section titles as headings** with `.accessibilityAddTraits(.isHeader)`, so VoiceOver users can jump between them. ## Decorative content An ornament, a divider drawn as an image, or artwork next to a title that already says the same thing should not be a stop for VoiceOver. Create it with `Image(decorative:)`, or add `.accessibilityHidden(true)` to a view that is not an image. Hiding is the intent, so express it directly; an empty label is not the same thing. ## Voice Control and localized labels A label serves more than VoiceOver. - **Voice Control** users activate a control by saying its name. Apple's documentation for `accessibilityInputLabels(_:)` says that without input labels, people can still refer to a view by its accessibility label, which is one more reason the label should match what is on screen. Add input labels only for extra names, such as "Skip" for "Next track". - **A string literal is a localization key.** `accessibilityLabel("Play")` resolves to the `LocalizedStringKey` overload, the same way `Text("Play")` does, so the label is translated along with the rest of the screen. How strings reach the String Catalog is covered with SwiftUI localization. ## Checking your work Turn on VoiceOver on a device and swipe through the screen. Every stop should say a short name, a role and, where relevant, a state. Each icon-only control is the first place to look, because it is where a missing name shows up first.

  • Why prefer Button("Play", systemImage:) with .labelStyle(.iconOnly) over an Image plus accessibilityLabel?
    The title is part of the view, so it is used wherever the system needs a name: VoiceOver reads it, a menu or toolbar can show it, and it is extracted for localization like any other string. An `accessibilityLabel` on a bare `Image` works for VoiceOver, but it is a second string to keep in sync, and it is the one a later refactor tends to drop.
  • The favorite button toggles between a heart and a filled heart; how does VoiceOver learn which state it is in?
    Keep the label constant ("Favorite") and expose the state separately, either with `.accessibilityAddTraits(isFavorite ? .isSelected : [])` or with `accessibilityValue`. Swapping the label between "Favorite" and "Unfavorite" makes VoiceOver announce a different control each time. Play and pause differ: the action itself changes, so the label changes with it. If the control really is an on/off switch, a `Toggle` with a custom `ToggleStyle` inherits Toggle's accessibility automatically.
  • When is accessibilityHint worth adding to a button?
    Only when the result of activating the control is not obvious from its label. Apple's documentation describes a hint as a brief phrase such as "Purchases the item". VoiceOver speaks it after a pause, and many people turn hints off, so the label must make sense without it; a hint on every button is noise.

  • Adding "button" to an accessibility label, as in "Play button"
  • Leaving an icon-only Button with just Image(systemName:) and no name
  • Using accessibilityHint to carry the control's name
  • Labelling a decorative ornament instead of using Image(decorative:)
  • Renaming a favorite button Favorite or Unfavorite instead of exposing selected state
open as a page

In SwiftUI, how does accessibilityElement(children:) with .combine, .contain or .ignore change what VoiceOver reads for a music track row?

level: middleimportance: must knowfreq 62%

basics

~10 s

accessibilityElement(children:) turns a container into one element: .combine merges the children's labels into it, .contain keeps them as separate children inside a group, and .ignore hides them so you must supply the label yourself.

open as a page

In SwiftUI, how do @ScaledMetric and the dynamicTypeSize environment value keep a now-playing header usable at the largest accessibility text sizes?

level: middleimportance: must knowfreq 64%

basics

~20 s

Text in text styles scales with Dynamic Type; fixed numbers do not. @ScaledMetric scales a number, such as an artwork size, along a text style's curve, and reading dynamicTypeSize lets a layout stack vertically when isAccessibilitySize is true.

open as a page

In SwiftUI, how does a view read the Reduce Motion, Increase Contrast and Differentiate Without Color settings, and what should it change for each?

level: middleimportance: should knowfreq 38%

basics

~20 s

SwiftUI exposes these settings as read-only environment values: accessibilityReduceMotion, colorSchemeContrast and accessibilityDifferentiateWithoutColor. Read them with @Environment and respond: replace large motion with a fade, strengthen borders and contrast, and add shapes or glyphs where color alone carries meaning.

open as a page

In SwiftUI, a custom circular volume knob works by drag but VoiceOver cannot change it; how do you fix it with accessibilityAdjustableAction or accessibilityRepresentation?

level: seniorimportance: should knowfreq 42%

basics

~20 s

A drag gesture is invisible to VoiceOver. Make the knob one element with a label and a value, then either add accessibilityAdjustableAction to handle increment and decrement, or use accessibilityRepresentation to let a hidden Slider describe it.

open as a page

As the iOS lead, what accessibility standard must a SwiftUI feature meet before it ships, and how does the team check it without slowing every release?

level: principalimportance: should knowfreq 30%

basics

~20 s

Write the standard as checkable SwiftUI outcomes: labelled controls, rows read as one element, adjustable custom controls, layouts that survive accessibility5, honoured motion settings. Then tier the checks: review and previews per change, a VoiceOver pass per feature, deeper audits per release.

open as a page