<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/Preview(_:traits:_:body:)",
  "metadataVersion" : "0.1.0",
  "role" : "Macro",
  "symbol" : {
    "kind" : "Macro",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI7Preview_6traits_4bodyySSSg_21DeveloperToolsSupport0C5TraitVyAfBV10ViewTraitsOGALdAA0J0_pyScMYcctcfm"
  },
  "title" : "Preview(_:traits:_:body:)"
}
-->

# Preview(_:traits:_:body:)

Creates a preview of a SwiftUI view using the specified traits.

```
@freestanding(declaration) macro Preview(_ name: String? = nil, traits: PreviewTrait<Preview.ViewTraits>, _ additionalTraits: PreviewTrait<Preview.ViewTraits>..., @ContentBuilder body: @escaping @MainActor () -> any View)
```

## Parameters

`name`

An optional display name for the preview. If you don’t specify a
name, the canvas labels the preview using the line number where the
preview appears in source.

`traits`

A
<doc://com.apple.documentation/documentation/DeveloperToolsSupport/PreviewTrait>
instance that customizes the appearance of the preview.

`additionalTraits`

Optional additional traits that further customize
the preview.

`body`

A [`ContentBuilder`](/documentation/SwiftUI/ContentBuilder) that produces a SwiftUI view to preview. You
typically specify one of your app’s custom views and optionally any
inputs, model data, modifiers, and enclosing views that the custom
view needs for normal operation.

## Overview

This macro behaves like [`Preview(_:body:)`](/documentation/SwiftUI/Preview(_:body:)) except that it also
enables you to customize the appearance of the preview by adding
one or more traits, which are instances of
<doc://com.apple.documentation/documentation/DeveloperToolsSupport/PreviewTrait>.
For example, you can display a preview at a fixed size using the
<doc://com.apple.documentation/documentation/DeveloperToolsSupport/PreviewTrait/fixedLayout(width:height:)>
trait:

```
#Preview(
    "Content",
    traits: .fixedLayout(width: 100, height: 100)
) {
    ContentView()
}
```

The macro ignores traits that don’t apply to the current context.
For example, the
<doc://com.apple.documentation/documentation/DeveloperToolsSupport/PreviewTrait/portrait>
trait has no impact on a visionOS preview.

Other preview macros provide different customization options. For example,
if you want to specify a custom viewpoint for the preview, use [`Preview(_:traits:body:cameras:)`](/documentation/SwiftUI/Preview(_:traits:body:cameras:)).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)