In SwiftUI, a custom circular volume knob works by drag but VoiceOver cannot change it; how do you fix it with accessibilityAdjustableAction or accessibilityRepresentation?
answer
- VoiceOver cannot perform your drag
- swipe up and down to adjust
- label plus a changing value
- a hidden Slider stand-in
basics
~20 sA 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.
solid answer
~40 sVoiceOver cannot see a `DragGesture`, and SwiftUI declares no `SliderStyle` to restyle a `Slider` into a knob, so the knob is a custom drawing with no accessibility of its own. There are two fixes. The manual one: `.accessibilityElement()`, `.accessibilityLabel("Volume")`, an `accessibilityValue` such as "60%", and `.accessibilityAdjustableAction`, whose handler receives `.increment` or `.decrement` when the user swipes up or down; in Swift 6 mode the switch also needs `@unknown default`. The declarative one: `.accessibilityRepresentation { Slider(value: $volume, in: 0...1) { Text("Volume") } }`. SwiftUI hides that slider and makes it non-interactive, then uses it only to generate the accessibility elements, so the knob reads and adjusts like a standard slider while the drawing stays custom. I prefer the representation when a standard control matches the semantics.
code
swift · 26 linesimport SwiftUI
struct VolumeKnob: View {
@Binding var volume: Double // 0...1
var body: some View {
Circle()
.fill(.quaternary)
.overlay(alignment: .top) { Capsule().frame(width: 4, height: 18).padding(.top, 8) }
.rotationEffect(.degrees(-135 + 270 * volume))
.frame(width: 120, height: 120)
.gesture(DragGesture().onChanged { drag in
volume = min(max(volume - drag.velocity.height / 20_000, 0), 1)
})
.accessibilityElement()
.accessibilityLabel("Volume")
.accessibilityValue(Text(volume, format: .percent.precision(.fractionLength(0))))
.accessibilityAdjustableAction { direction in
switch direction {
case .increment: volume = min(volume + 0.1, 1)
case .decrement: volume = max(volume - 0.1, 0)
@unknown default: break
}
}
}
}go deeper
Recall that VoiceOver cannot perform custom gestures, and that a custom control needs a label, a value and a way to adjust it.
Explain what accessibilityAdjustableAction receives, what accessibilityRepresentation does with the view you give it, and why the value must update with the state.
Choose between the two fixes by semantics, verify with VoiceOver on a device that swipes change and announce the value, and handle the non-frozen direction enum in Swift 6 mode.
Push custom controls toward standard counterparts with representations, so the design system's knobs, steppers and switches are accessible by construction rather than audited one by one.
## Why the knob is invisible A circular volume knob is usually a `Circle` with an indicator, rotated by a `DragGesture`. That works for touch, but **VoiceOver cannot perform a custom gesture**: it moves focus with swipes and activates with a double-tap. A drawn shape also has no label, no value and no way to be adjusted, so VoiceOver either skips the knob or lands on something that says nothing. The ideal fix would be a restyled standard control, because standard controls carry their accessibility with them. Apple's documentation says a custom `ToggleStyle` inherits Toggle's accessibility automatically. **SwiftUI declares no style protocol for `Slider`**, though, so a knob cannot be a restyled slider. That leaves two fixes. ## Fix 1: describe the element and handle adjustment Build the element by hand: 1. **`.accessibilityElement()`** turns the drawing into one element. With no argument, the children behaviour is `.ignore`, which is right here because the shapes have nothing useful to say. 2. **`.accessibilityLabel("Volume")`** names it. 3. **`.accessibilityValue(...)`** reports the current state, such as "60%", formatted from the bound value so it updates with every change. 4. **`.accessibilityAdjustableAction { direction in ... }`** handles adjustment. Its handler receives an `AccessibilityAdjustmentDirection`: **`.increment`** when the user swipes up and **`.decrement`** when they swipe down. The modifier has been available since iOS 13. `AccessibilityAdjustmentDirection` is not a frozen enum. The iOS 27 compiler warns that a switch over its two cases "may have additional unknown values" and that this "is an error in the Swift 6 language mode", so add `@unknown default`. Choose a step that a person can live with. Ten swipes from silent to full is reasonable; a hundred is not. ## Fix 2: let a standard control stand in `accessibilityRepresentation(representation:)`, available since iOS 15, replaces a view's accessibility elements with the elements of a view you supply: ```swift KnobFace(volume: volume) .accessibilityRepresentation { Slider(value: $volume, in: 0...1, step: 0.1) { Text("Volume") } } ``` Apple's documentation states the key facts: - SwiftUI **hides** the view in the closure and makes it **non-interactive**. - It uses that view **only to generate accessibility elements**. - Its own example is exactly this case: a custom adjustable control represented by a `Slider`. Because the slider is bound to the same state, a VoiceOver swipe changes `volume`, and the knob redraws. The label, the value and the adjustability all come from the standard control. ## Choosing between them | Concern | Adjustable action | Representation | |---|---|---| | Code you write | Label, value, step logic, unknown-case handling | One standard control | | Semantics | Whatever you describe | Exactly the stand-in's | | Fits | Controls with no standard counterpart | Controls that behave like a slider, toggle or picker | | Risk | Forgetting the value, or a step that is too fine | A stand-in whose meaning differs from the control | Prefer the representation when a standard control has the same meaning. Write the element yourself when it does not, such as a two-dimensional pad; there, `accessibilityChildren` can expose several synthetic elements. ## Keeping the value honest The value is what VoiceOver users actually experience, so treat it as part of the control's design. - **Clamp in the handler.** `min(volume + 0.1, 1)` and `max(volume - 0.1, 0)` keep repeated swipes inside the range; without them the knob reports values like 110%. - **Format with a format style**, such as `Text(volume, format: .percent.precision(.fractionLength(0)))`, so the spoken value follows the person's locale. Format styles themselves are covered with SwiftUI text. - **Report what the person cares about.** For a volume knob that is a percentage; for a tempo knob it might be "120 beats per minute". The raw `Double` is rarely the right thing to speak. ## Mistakes that ship - **Adding `.isButton`** so a double-tap does something. A double-tap cannot express "louder" or "quieter", and the knob then claims to be a button. - **Putting the value in the label**, as in "Volume 60%". The label should stay constant; the value changes. - **Forgetting the value entirely**, so a swipe changes the sound and VoiceOver says nothing. - **Expecting the representation to be visible.** It never draws; the knob's own drawing is all anyone sees. ## Verifying it Turn on VoiceOver, swipe to the knob, and confirm that it reads its label and value. Then swipe up and down: the value should change in sensible steps and be announced each time. Repeat at the minimum and maximum, where a missing clamp shows up as a value outside the range.
- Why does the adjustable-action version also need accessibilityValue?The action changes the volume, but VoiceOver speaks only what the element exposes. Without a value, a swipe up changes the sound and VoiceOver says nothing useful. Apple's guidance is that the value describes the current state when it differs from the label, here "60%" for an element labelled "Volume", and it is recomputed in `body` each time the volume changes.
- When is accessibilityRepresentation the wrong tool?When no standard control matches the semantics, for example a two-dimensional color wheel. A slider would then describe something the control is not. Build the element yourself with a label, value and actions, or with `accessibilityChildren` for several synthetic elements, as Apple's `Canvas` example does.
- If the knob had been a custom on/off switch instead, what would you do?Use a `Toggle` with a custom `ToggleStyle`. Apple's documentation says a custom toggle style inherits Toggle's accessibility automatically, so the label, the on/off value and the double-tap to change it come for free. `accessibilityRepresentation` is the fallback for controls whose standard counterpart offers no style protocol, as with `Slider`.
- Assuming VoiceOver users can perform the knob's drag gesture
- Adding the .isButton trait so a double-tap changes the volume
- Putting the current volume into the label instead of the value
- Expecting the Slider inside accessibilityRepresentation to be drawn on screen
- Believing a switch over both adjustment directions needs no @unknown default