TechnologiesSwiftUI

WorldScalingCompensation struct

iOSmacOStvOSwatchOSvisionOS✓ renders

Indicates whether returned metrics will take dynamic scaling into account.

How it works

WorldScalingCompensation describes how SwiftUI reconciles a view's logical size with its physical size when content is presented in world space, such as inside a volume or an immersive scene. In flat windows a point maps to a fixed scale, but once content lives in the shared 3D space its on-screen size depends on distance and the dynamic scaling the system applies, so a view that looks correct up close can read as oversized or undersized as it moves away from the viewer. Use WorldScalingCompensation to declare whether a view should keep its intended physical dimensions regardless of that system scaling, or follow the surrounding world scale. Reach for it when precise real-world sizing matters — measurement overlays, controls anchored to physical objects, or text that must stay legible at a chosen physical size.

  1. Read WorldScalingCompensation as a behavior value

    WorldScalingCompensation is a small value type whose cases express the compensation policy you want the system to apply to a view's scale in world space. You don't construct it imperatively in the layout body; you select one of its values and hand it to SwiftUI, which then decides how the rendered size of content like the Image(systemName: "globe") and Text("World Scaling") relates to physical dimensions.

  2. Choose between compensated and automatic scaling

    The type distinguishes content that should resist dynamic world scaling — preserving its specified physical size — from content that should scale with its surroundings. Picking the compensating behavior pins the VStack(spacing: 12) so its glyph and labels hold a consistent real-world size as the viewer's distance changes, while the default behavior lets the same stack grow and shrink with the world.

  3. Apply it to the subtree, not the leaf

    WorldScalingCompensation is supplied through SwiftUI's modifier and environment machinery so it cascades to a whole view subtree. Attaching it at the root of WorldScalingCompensationDemo.body means the choice governs every descendant — the .font(.headline) title and the .foregroundStyle(.secondary) caption alike — rather than forcing you to annotate each child individually.

  4. Let padding and intrinsic size define what gets compensated

    Compensation acts on the resolved physical extent of the content, so the intrinsic sizing you express still matters. The .font(.system(size: 44)) symbol and the .padding() around the stack establish the dimensions WorldScalingCompensation then preserves or releases, which is why the policy reads most clearly on content that has a deliberate, measurable size.

Try it — Switch WorldScalingCompensation from its compensating value to the automatic one on the root of WorldScalingCompensationDemo.body, then view the scene at increasing distance — the globe and labels will start tracking the world scale instead of holding their fixed physical size.

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.

WorldScalingCompensation.swift
struct WorldScalingCompensationDemo: View {
    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "globe")
                .font(.system(size: 44))
                .foregroundStyle(.blue)
            Text("World Scaling")
                .font(.headline)
            Text("Compensates content scale to the world space.")
                .font(.caption)
                .foregroundStyle(.secondary)
                .multilineTextAlignment(.center)
        }
        .padding()
    }
}
Live preview
World Scaling Compensates content scale to the world space.
World Scaling Compensates content scale to the world space.
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →