One Logo Pool Ball
SwiftUI · IOS API REFERENCE

PreviewModifier

protocol · iOS 18.0+ · SDK 27.1

@MainActor protocol PreviewModifier

A type that defines an environment in which previews can appear. Conforming types can define shared contexts that will be cached by the preview system, then reused across participating previews. For example, you might create a model container here and populate it with sample data; in your `body` method you would then apply it to the preview using the `.modelContainer` view modifier. ``` struct SampleData: PreviewModifier { static func makeSharedContext() throws -> ModelContainer { let container = try ModelContainer(for: Snack.self) container.mainContext.insert(Snack.potatoChips) return container } func body(content: Content, context: ModelContainer) -> some View { content.modelContainer(context) } } ``` Use the `.modifier` preview trait to attach modifiers to a preview. ``` #Preview(traits: .modifier(SampleData())) { @Previewable @Query var snacks: [Snack] return SnackView(snack: snacks.first!) } ```

Members (0)

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

Skipped declarations

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

PreviewModifier.makeSharedContext()

Static protocol requirements cannot be called through an instance.

All SwiftUI types →