TechnologiesSwiftUI

WorldAlignmentBehavior struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A type representing the world alignment behavior for a scene.

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.

  1. 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.

  2. 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 padded VStack, declaring how that content should orient itself in the world.

  3. Reference the gravityAligned case

    .gravityAligned is 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.

  4. 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 the Text beneath the headline.

Try it — Change 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.

WorldAlignmentBehavior.swift
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)
    }
}
Live preview
World Alignment {String(describing: behavior)}
World Alignment {String(describing: behavior)}
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →