One Logo Pool Ball
SwiftUI · IOS API REFERENCE

Scene

protocol · iOS 14.0+ · SDK 27.1

@MainActor @preconcurrency protocol Scene

A part of an app's user interface with a life cycle managed by the system. You create an ``SwiftUI/App`` by combining one or more instances that conform to the `Scene` protocol in the app's ``SwiftUI/App/body-swift.property``. You can use the built-in scenes that SwiftUI provides, like ``SwiftUI/WindowGroup``, along with custom scenes that you compose from other scenes. To create a custom scene, declare a type that conforms to the `Scene` protocol. Implement the required ``SwiftUI/Scene/body-swift.property`` computed property and provide the content for your custom scene: struct MyScene: Scene { var body: some Scene { WindowGroup { MyRootView() } } } A scene acts as a container for a view hierarchy that you want to display to the user. The system decides when and how to present the view hierarchy in the user interface in a way that's platform-appropriate and dependent on the current state of the app. For example, for the window group shown above, the system lets the user create or remove windows that contain `MyRootView` on platforms like macOS and iPadOS. On other platforms, the same view hierarchy might consume the entire display when active. Read the ``SwiftUI/EnvironmentValues/scenePhase`` environment value from within a scene or one of its views to check whether a scene is active or in some other state. You can create a property that contains the scene phase, which is one of the values in the ``SwiftUI/ScenePhase`` enumeration, using the ``SwiftUI/Environment`` attribute: struct MyScene: Scene { @Environment(\.scenePhase) private var scenePhase // ... } The `Scene` protocol provides scene modifiers, defined as protocol methods with default implementations, that you use to configure a scene. For example, you can use the ``SwiftUI/Scene/onChange(of:perform:)`` modifier to trigger an action when a value changes. The following code empties a cache when all of the scenes in the window group have moved to the background: struct MyScene: Scene { @Environment(\.scenePhase) private var scenePhase @StateObject private var cache = DataCache() var body: some Scene { WindowGroup { MyRootView() } .onChange(of: scenePhase) { newScenePhase in if newScenePhase == .background { cache.empty() } } } } A type conforming to this protocol inherits `@preconcurrency @MainActor` isolation from the protocol if the conformance is included in the type's base declaration: struct MyCustomType: Transition { // `@preconcurrency @MainActor` isolation by default } Isolation to the main actor is the default, but it's not required. Declare the conformance in an extension to opt out of main actor isolation: extension MyCustomType: Transition { // `nonisolated` by default }

Members (0)

This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.

Skipped declarations

30 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

Scene.restorationBehavior(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.windowResizability(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.windowManagerRole(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.defaultSize(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.defaultSize(width:height:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.environment(_:_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.transformEnvironment(_:transform:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.environment(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

Scene.environmentObject(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

Scene.defaultAppStorage(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.handlesExternalEvents(matching:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.persistentSystemOverlays(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.onChange(of:initial:_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.onChange(of:initial:_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.onChange(of:perform:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchTitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchTitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchTitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchTitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchSubtitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchSubtitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchSubtitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.documentLaunchSubtitle(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.commands(content:)

This declaration needs Swift generic type arguments. (no canonical witness for Content)

Scene.documentBrowserContextMenu(_:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.commandsRemoved()

This protocol needs associated types that cannot yet cross the bridge.

Scene.commandsReplaced(content:)

This declaration needs Swift generic type arguments. (no canonical witness for Content)

Scene.backgroundTask(_:action:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.windowToolbarLabelStyle(fixed:)

This protocol needs associated types that cannot yet cross the bridge.

Scene.windowToolbarLabelStyle(_:)

This protocol needs associated types that cannot yet cross the bridge.

All SwiftUI types →