Create a project

The project file

  • Appdelegate. swift – This is responsible for starting and terminating the App, and is responsible for communicating withSceneDelegateHandover.
  • Scenedelegate. swift – This is responsible for managing the application life cycle.
  • Contentview.swift — Most importantly, this is where the UI interface is written.
  • Assets.xcassets – Holds all the images and colors used in the project.
  • Launchscreen.storyboard – The screen that displays when the application loads.
  • Info.plist – Property list file that contains many configurations for the project, such as App name, version, etc
  • Preview Content – This is a folder that contains onePreview Assets.xcassets

Start the process

  1. AppDelegateThrough theapplication(_:configurationForConnecting:options)Returns aUISceneConfigurationThe instance
  2. Upon completion of the startup, control is transferred toSceneDelegate, it’sscene(_:willConnectTo:options:)It’s going to be called, setwindowThe root view controller of
  3. Is initialized when the root view controller is initializedContentViewIn theViewCome out

How does the ContentView appear on the screen?

In scenedelegate. swift you will see this code:

/ / create the window
let window = UIWindow(windowScene: windowScene)
// Set the Window rootViewController
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()
Copy the code
  • In setting upwindowtherootViewControllerIs initializedContentView, the UI interface can be displayed
  • This code should be familiar, in theUIKitPure code build iOS applications, will often be inAppDelegateWrite similar code in therootViewControllerforUIHostingControllerThe type,UIHostingControllerisUIViewControllerIs responsible for accepting a subclass ofSwiftUItheViewAnd use itUIKitTo render

ContentView.swift

import SwiftUI

struct ContentView : View {
    var body: some View {
        Text("Hello SwiftUI")}}struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()}}Copy the code
  • Interface descriptionContentViewBefore, we were inViewControllerUse code or leverageStoryBoard,XIBThings to do with the layout interface are now an inheritance fromViewThe structure is done
  • inContentViewInside, there’s onebody, returns a new Swift5.1Opaque return type, which means to return somethingView, butbodyYou don’t have to worry about the specifics
  • Pay attention to:bodyYou can’t return muchViewOr not return anyView, the Swift compiler will always report an error, so keep that in mindbodyOne must always be returnedView
  • inbodyIn theText("Hello SwiftUI"), indicating that the text is createdHello SwiftUIThe label of the
  • The final structureContentView_Previews, andContentViewSimilarly, it is specifically used to display a preview view in Xcode.

Previews

Xcode 11 + macOS 10.15 gives you a real-time preview of the SwiftUI interface. This preview helps you quickly see what code looks like without running it.

  • Supports multi-device preview
struct ContentView_Previews: PreviewProvider {
   static var previews: some View {
      Group {
         ContentView(a)// Preview the device
            .previewDevice(PreviewDevice(rawValue: "iPhone 11"))
            // Preview the device name
            .previewDisplayName("iPhone 11")

         ContentView()
            .previewDevice(PreviewDevice(rawValue: "iPhone 11 Pro Max"))
            .previewDisplayName("iPhone 11 Pro Max")}}}Copy the code
  • Supports multi-mode preview
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            // Large text
            ContentView()
                .environment(\.sizeCategory, .accessibilityExtraExtraExtraLarge)
            // Dark mode
            ContentView()
                .environment(\.colorScheme, .dark)
            // Navigation view
            NavigationView {
                ContentView()}}}}Copy the code

SwiftUI Video Tutorials

I have recorded a set of SwiftUI video tutorial, welcome your criticism and advice.