跳转至
Enumeration

PreviewLayout

The size constraint for a preview.

Declaration

enum PreviewLayout

Overview

Use this enumeration along with the previewLayout(_:) view modifier to change the way a view is displayed in a preview.

This enumeration has 3 different cases:

  1. device: Center the preview in the specified device.
  2. sizeThatFits: Fix the preview to the view size.
  3. fixed: Center the preview in a fixed size container.

In the following example, we fit a preview box to the text view.

struct ContentView: View {
    var body: some View {
        Text("➡️This box fits me snugly⬅️")
    }
}

struct ContentView\_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .previewLayout(.sizeThatFits)
    }
}
An image of a text view in preview displaying the string "This box fits me snugly" outlined by a gray rectangle with no empty space on any side of the string.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Case

device Center the preview in a container the size of the device on which the preview is running.

fixed Center the preview in a fixed size container.

sizeThatFits Fit the container to the size of the preview when offered the size of the device on which the preview is running.