Download

Xcode Previews. The #Preview macro and the canvas

How to add a preview, which modes the canvas has, how to preview on your own iPhone and what to do when Xcode says the preview crashed.

In short

Xcode Previews are a live picture of a screen on the canvas next to your code, refreshed on every edit without running the whole app. Since Xcode 15 in 2023 previews are declared with the #Preview macro for SwiftUI, UIKit and AppKit, and since Xcode 16 they run on a new engine that shows edits up to 30% faster.

What previews and the canvas are

A preview lets you see a screen while you write it. Xcode builds only what's needed, runs the screen apart from the app and shows it on the canvas to the right of your code. Change a padding or a color, and a second later the canvas shows it. In live mode the screen responds to taps, animations and text entry much like the simulator.

Previews used to be declared as a separate struct conforming to PreviewProvider. Xcode 15, which Apple showed in June 2023, replaced that with the #Preview macro, and Control-clicking an old struct lets Xcode convert it for you (Xcode 15 release notes). Per the SwiftUI documentation the macro itself is available back to iOS 13, so it works in older projects too.

Where previews live in Xcode

The canvas opens with the Show Canvas button in the upper-right corner of the project window or with Editor › Canvas. If the preview is paused, the Restart button on the canvas runs it. When a file has several macros, tabs with their names appear at the top of the canvas.

Mode buttons sit in the lower-left corner. Live is the interactive mode and the default for new previews. Selectable highlights the code for an element you double-click on the canvas. Variants shows orientations, text sizes and iPhone Duo side by side, and Resizable lets you drag the screen edges. iPhone Duo joined these variants in September 2026. In the lower-right corner you pick a device, set Device Settings such as dark appearance, orientation and Dynamic Type, and capture the screen with Screenshot (Xcode documentation).

Versions and features by year

WhatSince
#Preview macro for SwiftUI, UIView, UIViewController, AppKit and widgetsXcode 15, 2023
Previews of UIKit and AppKit screensonly on iOS 17 and macOS 14 and later
Device picker on the canvas, previews on a connected iPhoneXcode 15
New engine sharing build products with Build and RunXcode 16, edits up to 30% faster
@Previewable for @State and other wrappersXcode 16, SwiftUI only
PreviewModifier, shared data across previewsXcode 16
#Preview without return for SwiftUIXcode 16
iPhone Duo on the canvasXcode 27, September 2026

Per the Xcode 15 and Xcode 16 release notes and Xcode updates, checked October 17, 2026.

What Xcode Previews gained since 2023Timeline from 2023 to 2026. Since Xcode 15, shown in June 2023, previews are declared with the #Preview macro for SwiftUI, UIKit, AppKit and widgets, and the canvas lets you pick a device, including a connected iPhone. Since Xcode 16 in June 2024 previews run on a new engine that shares build products with Build and Run, edits show up to 30% faster, and the @Previewable macro and PreviewModifier protocol arrived. In September 2026, with Xcode 27, the canvas can show iPhone Duo. Source developer.apple.com.What Xcode Previews gained since 2023Per Xcode 15 and 16 release notes and Xcode updates, checked October 17, 2026The #Preview macroXcode 15, SwiftUI, UIKit, AppKit, widgetsPreviews on your iPhoneXcode 15, device picker on the canvasNew preview engineXcode 16, edits up to 30% faster@Previewable and PreviewModifierXcode 16, state and shared dataiPhone Duo on the canvasXcode 27, September 20262023202420252026
In three years previews went from a separate protocol to one macro that can hold state inline. Per the Xcode 16 release notes and Xcode updates, checked October 17, 2026.

How to add a preview, step by step

  1. Open the screen's file and below its declaration write #Preview { ContentView() }. For UIKit, create and return your UIViewController inside the macro.
  2. Click Show Canvas. If the canvas is paused, click Restart or press Option-Command-P.
  3. If the screen needs a binding, declare the state right in the macro, @Previewable @State var isOn = true, and pass it in.
  4. Add a second, named macro such as #Preview("Empty list") to see both states as tabs.
  5. Put images and sample data for previews into the target's Development Assets on the General tab. They won't ship in the app.
  6. Pick your iPhone in the canvas device menu and check the screen on a real phone.

Why Xcode says the preview crashed

A preview crashes for the same reasons an app does, only the canvas reports it. Since Xcode 16 in 2024, per Apple's notes, the canvas reports non-crash errors faster. The diagnostics button on the canvas opens a detailed log. Four causes come up most often.

  • The screen didn't get its environment. If a view reads an object from the Environment and the preview never supplies it with the environment modifier, the preview crashes on first access.
  • No data store. A view with a SwiftData @Query needs a container, and a preview without modelContainer crashes. An in-memory container with sample records works well.
  • Heavy work while the view is created. Network calls, disk access and large objects slow previews down and make them time out. Apple recommends passing a view only the data it needs and sharing expensive objects across previews with PreviewModifier.
  • A resource missing from the preview build. An image or file loaded with a force unwrap turns up empty in previews. Put it in Development Assets.

Common mistakes

  • Expecting @Previewable in UIKit. Apple says the macro is SwiftUI-only, so UIKit previews set up state in plain code.
  • Previewing a UIKit screen on iOS 16. UIKit and AppKit previews run only on iOS 17 and macOS 14 and later, and an older runtime shows nothing.
  • Treating previews as a full run. The canvas shows one screen, while navigation, purchases and notifications are checked in the simulator or on a phone.

Appto writes screens in SwiftUI in a regular Xcode project, so the canvas and previews work there like in any other. Appto does its own checking in a real simulator run, walking through every screen and fixing what it finds, more on the code page.

Questions

What are Xcode Previews?

A live picture of a screen on the canvas next to your code. It refreshes as you edit, without running the whole app.

How do I open the preview canvas in Xcode?

Click Show Canvas in the upper-right corner of the window or choose Editor › Canvas. If the canvas is paused, click Restart or press Option-Command-P.

Do previews work with UIKit?

Yes, since Xcode 15 the #Preview macro shows UIView and UIViewController, but only on iOS 17 and later.

Can I run previews on my iPhone?

Yes. Since Xcode 15 you pick an iPhone connected to your Mac in the canvas device menu, and the preview runs on it.

What is @Previewable for?

It lets you declare @State right inside #Preview without a wrapper view. The macro arrived in Xcode 16 in 2024 and works only with SwiftUI.

Build your own app in Appto

Appto checks the niche on fresh App Store data, designs the screens, writes the code, builds the App Store page and submits to Apple from your account or shows you what to press in App Store Connect. Runs on your own Claude Code or Codex.

Download for Mac