SwiftUI

How to Use the New SwiftUI Preview Macro


The Preview feature in SwiftUI allows developers to see what their app will look like in real-time, without having to run the app on a device or simulator. This feature is incredibly useful for developers who want to quickly iterate on their designs and make sure everything looks and functions as intended. With the introduction of Macros in iOS 17, the Preview feature has become even more powerful and versatile, allowing for even more customization and flexibility. In this article, we’ll explore how to use the new Preview Macro in SwiftUI and take a look at some of its exciting new features.

The SwiftUI #Preview Macro

Prior to the introduction of the new #Preview macro, you define a structure that conforms to the PreviewProvider protocol to create a preview of a view. Here is an example:

With the #Preview macros, you tell Xcode to create a preview like this:

As you can see, #Preview simplifies the way we define previews. Optionally, you can pass the #Preview macro a name to configure the preview’s name.

swiftui-preview-basics

You can use this to set up a preview for any view as needed. Xcode will then render the preview, which will appear directly in the canvas.

Previewing Multiple Views

swiftui-preview-multiple-views

When using PreviewProvider, you can embed several views to preview using Group.

If you use the #Preview macro, you can define multiple blocks of #Preview sections. For example, to preview both ArticleListView and ArticleView, you can create two #Preview code blocks as follows:

Preview Views in Landscape Orientation

swiftui-preview-landscape

The #Preview macro has an optional traits parameter that allows developers to customize the orientation of the simulator. To preview views in landscape mode, you can pass .landscapeLeft or .landscapeRight to the traits parameter. Here is an example:

Preview without Device Frame and Fixed Layout

The traits parameters can take another value named .sizeThatFitsLayout so that you can preview the view without any device frame.

swiftui-preview-sizethatfits

On top of .sizeThatFitsLayout, you can also use .fixedLayout to preview the view in a specific size.

Writing UIKit Previews

The Preview feature is no longer limited to SwiftUI. Even if you use UIKit, you can set up a preview for your UIKit views or view controllers using the #Preview macro. To preview a view controller, you can instantiate it in the code block:

If your view controller is designed in the storyboard, you can also preview it using the macro. Here is the sample code snippet:

Assuming you have assigned a storyboard ID for the view controller, you can create the view controller using the instantiateViewController method. This is how you use #Preview to preview a UIKit view controller.

swiftui-preview-storyboard-view-controller

Summary

The Preview feature in SwiftUI allows developers to see what their app will look like in real-time, without having to run the app on a device or simulator. The #Preview macro introduced in iOS 17 makes the preview code cleaner and simpler. It has become even more powerful and versatile, allowing you to preview views developed in UIKit.

If you enjoy reading this tutorial, don’t forget to check out our Mastering SwiftUI book.

SwiftUI
A Beginner’s Guide to NavigationSplitView in SwiftUI for iOS 16
Tutorial
Building a Simple ARKit Demo with SceneKit in Swift 4 and Xcode 9
SwiftUI
Using ImageRenderer to Convert SwiftUI Views into Images
Shares