How it works
AccessibilityAttachmentModifier is the concrete modifier type that SwiftUI produces when you attach accessibility information to a view. You rarely name it directly; instead you call accessibility modifiers like accessibilityLabel(_:), accessibilityHint(_:), and accessibilityAddTraits(_:), each of which returns a view wrapped in this type. Its role is to carry the accessibility metadata you specify into the accessibility tree that VoiceOver and other assistive technologies read, so a view can describe itself meaningfully without changing how it looks. Reach for these modifiers whenever a view's visual presentation alone does not convey its purpose to someone navigating with assistive technology.
Supply a spoken label with accessibilityLabel(_:)
accessibilityLabel(_:) sets the short, localized string an assistive technology announces in place of the view's inferred description. The star image carries no inherent text, so
.accessibilityLabel("Favorite")gives VoiceOver something concrete to read, and on theText("4.8 rating")view.accessibilityLabel("Rated 4.8 out of 5 stars")replaces the terse on-screen text with a fully spoken phrase. Each call returns the view re-typed through AccessibilityAttachmentModifier.Add context with accessibilityHint(_:)
accessibilityHint(_:) attaches a secondary phrase that describes the result of acting on an element, announced after the label and after a pause. Here
.accessibilityHint("Marks this item as a favorite")tells the user what activating the star will do, which the label alone does not express.Describe behavior with accessibilityAddTraits(_:)
accessibilityAddTraits(_:) merges one or more AccessibilityTraits into the element so assistive technologies treat it correctly. Applying
.accessibilityAddTraits(.isButton)tells VoiceOver the star behaves like a button and should be announced and activated as one, even though it is rendered as a plain Image.Chain modifiers to accumulate metadata
Because each accessibility modifier returns a view wrapped in AccessibilityAttachmentModifier, the calls compose: the label, hint, and traits on the
Image(systemName: "star.fill")stack onto one another to form a single, fully described accessibility element rather than three separate ones.
.accessibilityValue("4.8") after the .accessibilityLabel("Rated 4.8 out of 5 stars") line and listen to how VoiceOver appends the value to the label, demonstrating that the modifier attaches to the same element.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct AccessibilityAttachmentModifierDemo: View {
var body: some View {
VStack(spacing: 16) {
Image(systemName: "star.fill")
.font(.largeTitle)
.foregroundStyle(.yellow)
.accessibilityLabel("Favorite")
.accessibilityHint("Marks this item as a favorite")
.accessibilityAddTraits(.isButton)
Text("4.8 rating")
.font(.headline)
.accessibilityLabel("Rated 4.8 out of 5 stars")
}
.padding()
}
}