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.
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.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 suppliedcontentandcontextand then return aWindowPlacementto drive the result.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) }, thecontextparameter is where you'd read those metrics to choose a position or derive a size.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.
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.
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()
}
}