Previewing a view with different traits
SwiftUI’s preview macro allows us to preview views with different traits. In this recipe, we will create a simple app that displays an image and some text, and we will preview it with different traits. This will allow us to have different variants of the view in the same canvas, switching among them by clicking on the preview buttons generated by Xcode.
Getting ready
Let’s create a SwiftUI app named PreviewingWithTraits
.
How to do it…
We will modify the ContentView
struct generated by Xcode and then add multiple preview macros to show the content with different traits. The steps are as follows:
- Open the
ContentView.swift
file and modify the body of theContentView
struct:struct ContentView: View { var body: some View { ZStack { Color.teal .frame(width:200, height: 150) VStack { Image(systemName: "globe") ...