TechnologiesSwiftUIPresentation and Dialogs

PresentationSizingContext struct

iOSmacOStvOSwatchOSvisionOSiOS 18.0+✓ renders

Contextual information about a presentation.

How it works

PresentationSizingContext supplies the environmental measurements SwiftUI uses to size a presented view, such as a sheet. It arrives as a parameter to the proposedSize(for:context:) method of a custom PresentationSizing type, carrying the dimensions of the space the presentation is being placed into. Reach for it when the framework's built-in presentation sizes don't fit your design and you need to compute a presentation's size relative to its surroundings rather than hard-coding fixed points.

  1. Receive the context from a PresentationSizing conformer

    PresentationSizingContext is never created by your code; SwiftUI hands it to you. You get access to it by conforming a type to PresentationSizing and implementing its sizing method. In the example, the nested struct HalfWidthSizing conforms to PresentationSizing so that it can participate in measuring the sheet.

  2. Read it as the context parameter of proposedSize(for:context:)

    The protocol requirement proposedSize(for root: PresentationSizingRoot, context: PresentationSizingContext) -> ProposedViewSize is where the context appears. The root parameter identifies the presentation being sized, while the context parameter is the PresentationSizingContext value you inspect to make a sizing decision.

  3. Use containerSize to size relative to the available space

    The key member is containerSize, the dimensions of the container the presentation will occupy. Because it exposes a width and height, you can derive a proportional size instead of a fixed one. The example computes context.containerSize.width / 2 and context.containerSize.height / 3 to make the sheet half as wide and a third as tall as its container.

  4. Return the computed size as a ProposedViewSize

    The method's job is to turn the context into a concrete proposal, so you wrap your derived values in ProposedViewSize and return them. Here that proposal becomes ProposedViewSize(width: context.containerSize.width / 2, height: context.containerSize.height / 3), the size SwiftUI then offers to the presented content.

  5. Install the sizing strategy with presentationSizing(_:)

    A PresentationSizing type only takes effect once it is attached to a presentation. The presentationSizing(_:) modifier connects your conformer to the presented view, which is where the context ultimately originates. In the example, .presentationSizing(HalfWidthSizing()) applies the custom strategy to the content of the sheet(isPresented:).

Try it — Change the divisor in context.containerSize.width / 2 to context.containerSize.width to size the sheet to the full container width and watch how the presentation grows in response to the context's reported dimensions.

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.

PresentationSizingContext.swift
struct PresentationSizingContextDemo: View {
    struct HalfWidthSizing: PresentationSizing {
        func proposedSize(for root: PresentationSizingRoot, context: PresentationSizingContext) -> ProposedViewSize {
            ProposedViewSize(width: context.containerSize.width / 2, height: context.containerSize.height / 3)
        }
    }

    @State private var showing = true

    var body: some View {
        VStack {
            Text("Custom presentation sizing")
        }
        .padding()
        .sheet(isPresented: $showing) {
            VStack(spacing: 12) {
                Text("Sized via PresentationSizingContext")
                    .font(.headline)
                Text("Width = container.width / 2")
                    .foregroundStyle(.secondary)
            }
            .padding()
            .presentationSizing(HalfWidthSizing())
        }
    }
}
Live preview
Custom presentation sizing Sized via PresentationSizingContext Width = container.width / 2
Custom presentation sizing Sized via PresentationSizingContext Width = container.width / 2
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →