12

SwiftUI Forms Tutorial

 4 years ago
source link: https://www.tuicool.com/articles/m2QFJ32
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Forms are a collection of user input controls. Forms can contain sections and will act as a container view. SwiftUI automatically adapts the controls when placed inside a form. In this tutorial a form is displayed containing some controls.SwiftUI requires Xcode 11 and MacOS Catalina, which can be downloaded at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project . In the template selector, select iOS as the platform, select the Single View App template, and then click Next . Enter SwiftUIFormsTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next . Choose a location to save the project on your Mac.

2AVrii7.png!web

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

QF3Ibe3.png!web

In the Project navigator, click to select ContentView.swift . Change the code inside the ContentView struct to

struct ContentView: View {
    
    // 1.
    @State private var enableAirplaneMode = false
      
    // 2.
    var notificationMode = ["Lock Screen", "Notification Centre", "Banners"]
    @State private var selectedMode = 0
    
    var body: some View {
        NavigationView {
            // 3.
            Form {
                // 4.
                Section(header: Text("General Settings")){
                    Toggle(isOn: $enableAirplaneMode) {
                        Text("Airplane Mode")
                    }
                    
                    Picker(selection: $selectedMode, label: Text("Notifications")) {
                        ForEach(0..<notificationMode.count) {
                            Text(self.notificationMode[$0])
                        }
                    }
                }
                
                // 5.
                Section(header: Text("About")) {
                    HStack {
                        Text("Name")
                        Spacer()
                        Text("iPhone 11")
                    }
                    
                    HStack {
                        Text("Software Version")
                        Spacer()
                        Text("13.1.1")
                    }
                }
            } .navigationBarTitle("Settings")
        }
    }
}
  1. A State property is declared ot display the state of the toggle view.

  2. An array is declared containing notification modes. The first mode is selected by the picker view using a state property.

  3. A form is declared. The form is a container view which contains two sections.

  4. The first section containts a header text, a toggle view and a picker view which will display the current selected notification mode.

  5. The second section is displayed containg text views inside a horizontal stack.

Go to the Preview window and start the Live view. The toggle and the picker view can be altered inside the form. The preview will look like this.

IZBvyu7.png!web

The source code of the SwiftUIFormsTutorial can be downloaded at the ioscreator repository on Github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK