TechnologiesSwiftUI

SurfaceSnappingInfo struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A type representing information about the window scenes snap state.

How it works

SurfaceSnappingInfo is a lightweight value type that captures the state of how a view aligns, or snaps, to a surface as it is positioned. SwiftUI hands you this information when content settles against a target so that you can react to where and how the snap occurred rather than computing those geometric relationships yourself. Reach for it when you need to read the result of a snapping interaction — for example, to update accompanying UI or drive feedback once a draggable element has come to rest against its surface.

  1. Create the value with SurfaceSnappingInfo()

    The type is a simple struct you can instantiate directly to obtain a default, empty snapping state. In the example, SurfaceSnappingInfo() produces the initial value that the view starts from before any snapping has taken place, giving you a concrete instance to observe and pass around.

  2. Hold it in observable state

    Because SurfaceSnappingInfo is a value type, it composes naturally with SwiftUI's state machinery. Storing it in @State private var info lets the view own the current snapping state, and any change to info triggers a re-render so the displayed information stays in sync with the latest snap.

  3. Read the snapping state in your layout

    The instance is meant to be inspected, not just stored. Here String(describing: info) renders the value into the caption Text("Info: \(String(describing: info))"), demonstrating how you surface the contents of a SurfaceSnappingInfo — in real use you would branch on its members to decide layout, styling, or feedback once content has snapped.

  4. Bind it to the element that snaps

    SurfaceSnappingInfo describes the relationship between a view and the surface it settles against, so it belongs alongside the snapping element. The RoundedRectangle labeled "Drag to snap" stands in for that draggable content, and the info value is the place you would record the outcome of its interaction with the surface.

Try it — Replace the SurfaceSnappingInfo() initializer in the @State private var info declaration with a configured instance and watch the String(describing: info) caption update to reflect the new snapping state.

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.

SurfaceSnappingInfo.swift
struct SurfaceSnappingInfoDemo: View {
    @State private var info = SurfaceSnappingInfo()
    var body: some View {
        VStack(spacing: 12) {
            Text("Surface Snapping")
                .font(.headline)
            RoundedRectangle(cornerRadius: 12)
                .fill(.blue.opacity(0.2))
                .frame(width: 200, height: 120)
                .overlay(Text("Drag to snap"))
            Text("Info: \(String(describing: info))")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}
Live preview
Surface Snapping Drag to snap Info: {String(describing: info)}
Surface Snapping Drag to snap Info: {String(describing: info)}
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →