TechnologiesSwiftUI

WindowPlacement struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A type which represents a preferred size and position for a window.

How it works

WindowPlacement is a value type that describes where a window appears on screen and, optionally, how large it is when the system first opens it. You return one from a scene's placement closure to take control of initial positioning instead of accepting the system's default. Reach for WindowPlacement when a window should open in a deliberate spot — centered on the active display, anchored to a corner, or sized to a fraction of the available space — rather than wherever the window manager would otherwise put it.

  1. Construct a placement from a position

    WindowPlacement is built by initializing it with a position descriptor that anchors the window relative to the available display area. In the example, WindowPlacement(.center) asks the system to open the window centered, which is the most common starting point before you layer on any sizing.

  2. Return it from the placement closure

    A WindowPlacement only takes effect when it is the value you hand back from a scene's placement resolver. The example sketches the .defaultWindowPlacement { content, context in ... } form, where you inspect the supplied content and context and then return a WindowPlacement to drive the result.

  3. Use the context to make the placement adaptive

    The placement closure receives a context describing the current environment — the display bounds and default size the system proposes — so your WindowPlacement can respond to the actual screen rather than hard-coded coordinates. In defaultWindowPlacement { content, context in WindowPlacement(.center) }, the context parameter is where you'd read those metrics to choose a position or derive a size.

  4. Combine position with an explicit size

    Beyond the position-only initializer shown, WindowPlacement lets you pair a position with a concrete size so the window opens both placed and dimensioned in one step. You extend the WindowPlacement(.center) call site with a size argument when centering alone isn't enough and the window needs a specific footprint on launch.

Try it — Change WindowPlacement(.center) in the .defaultWindowPlacement closure to a different position anchor and reopen the window to watch where it lands on screen.

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.

WindowPlacement.swift
struct WindowPlacementDemo: View {
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Window Placement")
                .font(.headline)
            Text(".defaultWindowPlacement { content, context in")
                .font(.caption.monospaced())
            Text("    WindowPlacement(.center)")
                .font(.caption.monospaced())
            Text("}")
                .font(.caption.monospaced())
        }
        .padding()
    }
}
Live preview
Window Placement .defaultWindowPlacement { content, context in WindowPlacement(.center) }
Window Placement .defaultWindowPlacement { content, context in WindowPlacement(.center) }
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →