skip to content

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%

answer

  1. one stop per row, not four
  2. merge, keep as children, or hide
  3. the default argument is .ignore
  4. named actions for buttons inside

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.

solid answer

~40 s

Without grouping, a track row with artwork, title, artist and duration is three or four VoiceOver stops, and a long playlist becomes tiring. `.accessibilityElement(children: .combine)` on the row's `HStack` makes one element whose properties are merged from the children, so VoiceOver reads the title, artist and length together. `.contain` keeps every child as its own element but groups them under the new element, which matters for navigation and rotors. `.ignore`, the default when you call `accessibilityElement()` with no argument, hides the children entirely, so you must add an `accessibilityLabel`. When the row holds its own buttons, I use `.ignore`, write the label myself and expose each button through `accessibilityAction(named:)`, so the row is one stop and its actions are in VoiceOver's actions list.

code

swift · 18 lines
swift
import SwiftUI

struct TrackRow: View {  // text and a decorative image: merge into one element
    let title: String, artist: String, length: Duration

    var body: some View {
        HStack {
            Image(decorative: "artwork-placeholder")
            VStack(alignment: .leading) {
                Text(title).font(.headline)
                Text(artist).font(.subheadline)
            }
            Spacer()
            Text(length, format: .time(pattern: .minuteSecond))
        }
        .accessibilityElement(children: .combine)
    }
}

go deeper

for a junior

Remember the three behaviours in one line each: .combine merges, .contain groups, .ignore hides. Remember also that calling accessibilityElement() with no argument means .ignore.

for a middle

Explain why a row should usually be one stop, how merged labels follow child order, and why a row with its own buttons needs a written label plus accessibilityAction(named:).

for a senior

Diagnose rows that read badly in production: duplicated text, a silent element after .ignore, buttons lost inside a merge. Fix them, and confirm with a VoiceOver swipe through a long list.

for a principal

Decide on one row pattern for every list cell type in the design system, so grouping and custom actions are built into shared components rather than added screen by screen.

## The problem: too many stops VoiceOver moves through a screen one **accessibility element** at a time. By default, SwiftUI turns each `Text`, labeled `Image` and control into its own element. A track row with artwork, a title, an artist and a duration therefore becomes three or four stops, and a playlist of fifty rows becomes two hundred swipes, with the duration of one track read separately from its title. Grouping fixes this, and SwiftUI does it with one modifier: ```swift func accessibilityElement(children: AccessibilityChildBehavior = .ignore) -> some View ``` Apple's documentation describes it as creating a new accessibility element, or changing the child behaviour of an existing one. Note the **default argument: `.ignore`**. ## The three behaviours `AccessibilityChildBehavior` has three values, available since iOS 13: | Behaviour | Apple's description | VoiceOver result for the row | |---|---|---| | `.combine` | Child elements' properties are merged into the new element | One stop that reads title, artist and duration together | | `.contain` | Child elements become children of the new element | Several stops, grouped under one container | | `.ignore` | Child elements become hidden | One stop with no label until you add one | - **`.combine`** is the everyday choice for a row of text and decorative images. The merged label follows the children's order, so arrange the views in the order they should be read. - **`.contain`** does not reduce the number of stops. It creates a container, which helps VoiceOver treat a group as a unit for navigation and is what Apple's rotor example applies to a scroll view. - **`.ignore`** throws the children's descriptions away. It is right when you want to write the whole description yourself, and wrong when you forget to. ## Rows that contain buttons A queue row with **Play Next** and **Remove** buttons is the hard case. If you leave it alone, each button is a separate stop, which is acceptable but slow. If you add `.combine`, the buttons' properties are merged into the row element, so what VoiceOver offers depends on how the merge treats them rather than on your design. The explicit pattern states the result directly: 1. Apply `.accessibilityElement(children: .ignore)` to the row. 2. Write the label: `.accessibilityLabel("\(track.title), \(track.artist)")`. 3. Add each operation with `.accessibilityAction(named: "Play next", playNext)` and `.accessibilityAction(named: "Remove", remove)`. VoiceOver then lands on the row once, reads one sentence, and offers the named actions in its actions list, where the user swipes up or down to pick one. The visible buttons still work for touch users, because accessibility modifiers change only what assistive technologies see. ## Related tools - **`accessibilitySortPriority(_:)`** reorders elements at the same level. Higher numbers are read first, and the default is zero. - **`accessibilityHidden(true)`** removes one view, such as a decorative divider, without regrouping anything. - **`accessibilityChildren(children:)`** replaces an element's children with synthetic elements you describe, which suits a `Canvas` drawing that has none. - **`accessibilityRotor(_:entries:entryLabel:)`** adds a custom rotor, such as "Favorites", so a VoiceOver user can jump between matching rows. Apple's documentation requires that the rotor be attached to a `ScrollView` or an element directly inside one, and that the entries' identifiers match the `ForEach` identities. ## Common mistakes - Calling `.accessibilityElement()` and expecting a merge, then shipping a row that VoiceOver lands on and reads nothing. - Using `.contain` to "group" a row and being surprised that every text is still a stop. - Combining a row whose children repeat each other, such as an artwork image labelled with the album title next to a `Text` of the same title, so the merged label says it twice. Make the image decorative first. - Combining a row that holds controls and losing track of how its actions surface. - Applying `.combine` to the whole list instead of to each row, which merges every track into one enormous element. Group at the level a person thinks of as one item. ## How to verify Swipe through the list with VoiceOver on. Each row should be one stop that reads like a sentence, and the actions you added should appear when you swipe up or down on it. If a row is silent, look for `.ignore` without a label; if it is chatty, look for a missing `.combine` or an image that should be decorative.

  • VoiceOver reads a combined row's parts in an awkward order; how do you change it?
    A combined element follows its children's order, so the first fix is the view order itself. When the visual order must stay, switch to `.ignore` and write the label in the order you want, or, for separate elements inside a container, use `accessibilitySortPriority`, where higher numbers are read first and the default is zero.
  • How would you let VoiceOver users jump between the favorited tracks in a long list?
    Add a custom rotor to the scroll view: `.accessibilityRotor("Favorites", entries: favoriteTracks, entryLabel: \.title)`. Apple's documentation says the entries' identifiers must match the `ForEach` identities in the `ScrollView`, and SwiftUI scrolls an entry into place when the user navigates to it, which also reaches rows a lazy stack has not built yet.
  • Why not simply add .combine to a row that contains two buttons?
    Combining merges the buttons into the row element, so the row stops being a clear set of controls and how each action surfaces depends on the merge rather than on your design. Writing the label and adding `accessibilityAction(named:)` for each operation states the result exactly, and the result no longer depends on how a merge treats nested controls.

Grouping a row is like a librarian reading a catalogue card aloud: title, author and shelf mark in one breath, not three separate cards. The named actions are the requests you can make at the desk once that card is in hand.

  • Thinking accessibilityElement() with no argument merges the children's labels
  • Believing .contain makes the row one VoiceOver stop
  • Using .ignore without then adding an accessibilityLabel
  • Leaving every Text in a long list as a separate VoiceOver stop
  • Making buttons inside a grouped row unreachable instead of adding named actions