var body: Self.Body
associatedtype Body : Scene
Availability 有効性
Technology
protocol Scene
You create an App
by combining one or more instances that conform to the Scene
protocol in the app’s body
. You can use the built-in scenes that SwiftUI provides, like Window
, 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 body
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 My
on platforms like macOS and iPadOS. On other platforms, the same view hierarchy might consume the entire display when active.
Read the scene
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 Scene
enumeration, using the Environment
attribute:
struct MyScene: Scene {
private var scenePhase (\.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 on
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 {
private var scenePhase (\.scenePhase)
private var cache = DataCache()
var body: some Scene {
WindowGroup {
MyRootView()
}
.onChange(of: scenePhase) { newScenePhase in
if newScenePhase == .background {
cache.empty()
}
}
}
}
var body: Self.Body
associatedtype Body : Scene
func onChange <V>(of: V, perform: (V) -> Void) -> some Scene
func handlesExternalEvents (matching: Set<String>) -> some Scene
func defaultAppStorage (UserDefaults ) -> some Scene
AppStorage
contained within the scene and its view content.
シーンとそれのビュー内容内に含まれるAppStorage
によって使われる省略時記憶装置(デフォルトストア)。
func commands<Content>(content: () -> Content) -> some Scene
func windowStyle <S>(S) -> some Scene
func windowToolbarStyle <S>(S) -> some Scene
DocumentGroup
Group
Content
conforms to Scene
.ModifiedContent
Content
conforms to Scene
and Modifier
conforms to _SceneModifier
.Settings
WKNotificationScene
WindowGroup
enum ScenePhase
struct SceneBuilder