SwiftUI Forms Tutorial

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.

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

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) {
                // 5.
                Section(header: Text("About")) {
                    HStack {
                        Text("iPhone 11")
                    HStack {
                        Text("Software Version")
            } .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.

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