Читать книгу SwiftUI For Dummies - Wei-Meng Lee - Страница 16

Generating different previews

Оглавление

Notice this block of code at the bottom of ContentView.swift?

struct ContentView_Previews: PreviewProvider {

static var previews: some View {

ContentView()

}

}

The ContentView_Previews struct conforms to the PreviewProvider protocol. This protocol produces view previews in Xcode so that you can preview your UI created in SwiftUI without needing to explicitly run the application on the iPhone Simulator or real devices. Essentially, it controls what you see on the preview canvas. As an example, if you want to preview how your UI will look like on an iPhone SE device, you can modify the ContentView_Previews struct as follows (see Figure 1-16):


FIGURE 1-16: Previewing the UI on two iOS devices — the latest iPhone and an iPhone SE.

struct ContentView_Previews: PreviewProvider {

static var previews: some View {

Group {

ContentView()

ContentView()

.previewDevice(PreviewDevice(

rawValue: "iPhone SE"))

.previewDisplayName("iPhone SE")

}

}

}

SwiftUI For Dummies

Подняться наверх