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

Automatically previewing your user interface using the canvas

Оглавление

By default, you should see the Inspector window on the right side of the Xcode window. For building your UI using SwiftUI, you usually don’t need the Inspector window, so you can dismiss it to gain more screen estate for previewing your UI using the canvas. To dismiss the Inspector window, click the button on the upper-right corner of Xcode (see Figure 1-8).


FIGURE 1-8: Dismissing the Inspector window.

With the Inspector window dismissed, you should now see the canvas on the right side of Xcode (see Figure 1-9). The canvas lets you preview the UI of your application without needing to run the application on the iPhone Simulator or a real device.

If you don’t see the canvas, you can bring it up again through the Editor ⇒ Canvas menu.

To preview your UI, click the Resume button on the canvas. You should now be able to see the preview (see Figure 1-10).

If you don’t see the Resume button, make sure you’re running macOS Catalina (10.15) or later.

Now let’s modify the ContentView.swift file with the code that you’ve seen earlier (see Figure 1-11).

You may notice that the automatic preview has paused. This sometimes happen when the file you're previewing has some changes that caused the containing module to be rebuilt. When that happens, click the Restore button, and you should see the preview again (see Figure 1-12).


FIGURE 1-9: The canvas allows you to preview your application without deploying it on the iPhone Simulator or a real device.


FIGURE 1-10: Previewing your app on the canvas.


FIGURE 1-11: Modifying the ContentView.swift file.


FIGURE 1-12: The preview is updated to reflect the changes in the code.

If you change the color of the Text view (within the Button view) to blue, you should see the changes automatically reflected in the preview:

Text("Submit")

.padding(EdgeInsets(

top: 10, leading: 10,

bottom: 10, trailing: 10))

.background(Color.blue)

The automatic update feature of preview doesn't always work. There are times where you have to click Try Again to rebuild the preview (see Figure 1-13).


FIGURE 1-13: Occasionally you have to click the Try Again button to update the preview.

SwiftUI For Dummies

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