How it works
OrnamentAttachmentAnchor describes where an ornament docks relative to a scene's content. In visionOS, an ornament is an auxiliary control surface — toolbars, playback controls, tab bars — that floats outside a window's bounds without crowding the primary content, and the anchor is what tells SwiftUI which edge or position that floating element should hug. Rather than measuring offsets by hand, you hand the framework a semantic location and it keeps the ornament correctly placed as the scene resizes or repositions. Reach for OrnamentAttachmentAnchor whenever you call the ornament(attachmentAnchor:contentAlignment:ornament:) modifier and need to say where the accessory belongs.
Pass an anchor to ornament(attachmentAnchor:ornament:)
The ornament modifier takes an OrnamentAttachmentAnchor as its attachmentAnchor argument and a view builder that produces the floating accessory. In the example,
.ornament(attachmentAnchor: .scene(.bottom))attaches the trailing closure's controls to the host view, which here is aText("Main Content")card.Choose a scene-relative position with .scene(_:)
The type vends ornaments through the static
.scene(_:)factory, which positions the ornament relative to the scene's bounds. The example uses.scene(.bottom), docking the accessory along the lower edge of the content; passing.top,.leading, or.trailinginstead moves it to the corresponding side.Supply a UnitPoint to place the ornament
.scene(_:)accepts a UnitPoint, so the anchor isn't limited to named edges — any normalized point such as.topLeading,.center, or.bottomTrailingworks, letting you pin the accessory to a precise spot. The example's.bottomis one of these standard UnitPoint values.Build the ornament content in the trailing closure
Whatever you return from the ornament's view builder rides along at the anchored location. Here the closure provides an
HStackofImage(systemName:)playback glyphs —play.fill,pause.fill, andstop.fill— which become the floating control strip the anchor positions.Finish the floating surface with glassBackgroundEffect()
Because the ornament sits outside the window's material, give its content its own backing so it reads as a distinct floating panel. The example applies
.glassBackgroundEffect()to the paddedHStack, producing the translucent capsule that the bottom anchor parks beneath the content card.
.scene(.bottom) to .scene(.top) and watch the glass playback strip jump to the upper edge of the content card.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 OrnamentAttachmentAnchorDemo: View {
var body: some View {
Text("Main Content")
.font(.title)
.padding(40)
.background(.regularMaterial, in: .rect(cornerRadius: 16))
.ornament(attachmentAnchor: .scene(.bottom)) {
HStack {
Image(systemName: "play.fill")
Image(systemName: "pause.fill")
Image(systemName: "stop.fill")
}
.padding()
.glassBackgroundEffect()
}
.padding()
}
}