From the course: iPadOS 13 Development Essential Training

Previewing multiple platforms in SwiftUI - iOS Tutorial

From the course: iPadOS 13 Development Essential Training

Start my 1-month free trial

Previewing multiple platforms in SwiftUI

- [Instructor] Another great thing about SwiftUI is that you can actually preview multiple platforms and devices at the same time. So let's say I wanted to see what my app looks like on an iPad and an iPhone at the same time. I can do that using the preview. So in one of these SwiftUI views, we have this guy at the bottom, ContentView_Previews. So this allows you to set up previews for your application. So the default just creates an instance of your struct, and that's going to show on the device that you have selected up here. So let's say I choose iPhone 11 right here, then you see the device gets updated in the right, and it takes a minute to build that preview. I'm going to go back to iPad 7th generation, and let's take a look at how to view both at the same time. I'm going to wrap this ContentView in a group. Just like that, so a group with some curly braces, and then we're going to put ContentView inside of there. Then we're going to create a copy of ContentView, and now we have two of 'em. So we have two iPads. So let's say we want to specify the device that's running in one of them. So maybe the top one, we'll keep at whatever we have selected up here, so whatever target we're looking at, we'll see that at the top. But the bottom view, let's keep it solid at an iPhone, so type a . right after that line, and then type in PreviewDevice, and I'm going to hit a ., and then I'm going to run the init that asks for a stringLiteral. Then I'm going to paste in the string, and I'm going to get the string just from here, so I can just put in iPhone 11 like it's written right here, and then that will show me that device. So iPhone 11. And there we go, that's what the app looks like on an iPhone 11. If I want to run on a different device, I can simply look at the name and change it, maybe I want to look at iPhone 8 Plus. And there we go. So of course, you can copy and paste these lines and preview on as many devices at once as you want, but for this course, I'm mainly going to stick to the iPad and iPhone 11. So to review, if you want to preview in multiple devices, create a group, and then just copy and paste your constructor right there, and then below the device you want to change, use .previewDevice, and then set the stringLiteral value to a value that you see in your simulator list.

Contents