Previewing the layout on different devices
SwiftUI allows developers to preview designs on multiple screen sizes and device types at the same time using the .previewDevice()
modifier. In this recipe, we will create a simple app that displays an image and some text on multiple devices.
Getting ready
Let's create a SwiftUI app called named PreviewOnDifferentDevices
.
How to do it…
We will add an image and some text to the ContentView
struct and modify the preview struct to show the content on multiple devices. The steps are as follows:
- Click on the Preview Content folder in the Xcode navigation pane.
- Import the
friendship.png
image from the Chapter 3 resource files (it can be found at https://github.com/PacktPublishing/SwiftUI-Cookbook/tree/master/Resources/Chapter03/recipe%202). - Click on the
ContentView.swift
file from the navigation pane to open it in the editor window. - Replace the initial
Text
view inContentView
struct's body variable...