How it works
WorldAlignmentBehavior describes how a view's content orients itself relative to the physical world in a spatial scene, rather than to a fixed coordinate space. It lets you state whether content should stay locked to real-world gravity, follow the device, or remain free of any world reference, so the system can keep it readable and stable as the viewer moves. Reach for it when you place SwiftUI content in a 3D or volumetric context and need predictable, intentional alignment instead of leaving orientation to chance.
Choose an alignment with a WorldAlignmentBehavior value
WorldAlignmentBehavior is a value type whose static members name the available alignment strategies. You select one and hold onto it, as in
let behavior: WorldAlignmentBehavior = .gravityAligned, which keeps content upright with respect to real-world gravity.Apply it with the worldAlignment(_:) modifier
The behavior takes effect only once attached to a view through the
worldAlignment(behavior)modifier. Here it sits at the end of the modifier chain on the paddedVStack, declaring how that content should orient itself in the world.Reference the gravityAligned case
.gravityAlignedis the strategy used in this example: it ties the content's vertical axis to physical gravity so it does not tip or roll as the surrounding scene shifts. Swapping in a different case changes the rule the system applies to the same view.Inspect the value as a description
Because WorldAlignmentBehavior is a plain value, you can surface the active choice for debugging or display. The example does this with
String(describing: behavior), rendering the selected behavior into theTextbeneath the headline.
let behavior: WorldAlignmentBehavior = .gravityAligned to a different case and watch both the worldAlignment(behavior) result and the String(describing: behavior) label update together.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 WorldAlignmentBehaviorDemo: View {
let behavior: WorldAlignmentBehavior = .gravityAligned
var body: some View {
VStack(spacing: 12) {
Image(systemName: "cube.transparent")
.font(.system(size: 44))
.foregroundStyle(.tint)
Text("World Alignment")
.font(.headline)
Text("\(String(describing: behavior))")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.padding()
.worldAlignment(behavior)
}
}