SwiftUI For Dummies. Wei-Meng Lee

SwiftUI For Dummies - Wei-Meng Lee


Скачать книгу
1-11: Modifying the ContentView.swift file.

The preview is updated to reflect the changes in the code.

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

       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).

Occasionally you have to click the Try Again button to update the preview.

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

      Working with Live Preview

Clicking the Live Preview button allows you to run your application directly on the canvas.

      FIGURE 1-14: Clicking the Live Preview button allows you to run your application directly on the canvas.

Testing your application in Live Preview mode.

      FIGURE 1-15: Testing your application in Live Preview mode.

      Generating different previews

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

       struct ContentView_Previews: PreviewProvider {

       static var previews: some View {

       ContentView()

       }

       }

Previewing the UI on two iOS devices — the latest iPhone and an iPhone SE.

      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")

       }

       }

       }

      Now that you've seen how to get started with SwiftUI, let’s take a moment to examine the various files created in the project and see how the various parts connect.

       AppDelegate.swift

       SceneDelegate.swift

       ContentView.swift (this is the file that you've been modifying to create the UI of your iOS application)

       Info.plist

Screenshot of the HelloSwiftUI page displaying the following files: AppDelegate.swift; SceneDelegate.swift; ContentView.swift; and Info.plist.

      FIGURE 1-17: The content of the project created.

      Info.plist

      Within the Application Scene Manifest key, you have the following keys:

       Enable Multiple Windows: This is set to NO by default. You can set this to YES if you're building apps for iPadOS and macOS.

       Application Session Role: An array that contains a list of dictionary objects. The default object contains a key named Delegate Class Name that points to the SceneDelegate.swift file.

Screenshot of the HelloSwiftUI page for examining the items in the Info.plist file displaying the Application Scene Manifest key that contains a list of dictionary objects.

      FIGURE 1-18: Examining the items in the Info.plist file.

      AppDelegate.swift

      AppDelegate.swift is the place where you write code to handle an application's launch, going into the background, coming to the foreground, and other activities.

      AppDelegate.swift has three main functions:

       application(:didFinishLaunchingWithOptions)


Скачать книгу