Category: 05. SwiftUI Basics

https://cdn3d.iconscout.com/3d/premium/thumb/ui-ux-3d-icon-png-download-9831982.png

  • SwiftUI Lists & Forms Swipe Actions

    Swipe Actions

    Add contextual actions with .swipeActions on list rows.


    Example: Trailing delete

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SwipeTrailingDemo: View {
      @State private var items = ["A","B","C"]
      var body: some View {
    
    List {
      ForEach(items, id: \.self) { item in
        Text(item)
          .swipeActions(edge: .trailing) {
            Button(role: .destructive) { items.removeAll { $0 == item } } label: { Label("Delete", systemImage: "trash") }
          }
      }
    }
    } }


    Example: Leading actions

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SwipeLeadingDemo: View {
      @State private var items = ["A","B","C"]
      @State private var favorites: Set<String> = []
      var body: some View {
    
    List {
      ForEach(items, id: \.self) { item in
        HStack { Text(item); if favorites.contains(item) { Image(systemName: "star.fill").foregroundStyle(.yellow) } }
          .swipeActions(edge: .leading) {
            Button { favorites.insert(item) } label: { Label("Favorite", systemImage: "star") }
            Button { /* share */ } label: { Label("Share", systemImage: "square.and.arrow.up") }
          }
      }
    }
    } }
  • SwiftUI Lists & Forms Inputs

    SwiftUI Lists & Forms Inputs

    Collect user input with TextFieldToggle, and Picker.


    Collect input with TextField, Toggle, and Picker

    Use form controls bound to @State to capture text, booleans, and choices.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct InputsDemo: View {
      @State private var name = ""
      @State private var enabled = true
      @State private var choice = 1
      var body: some View {
    
    Form {
      TextField("Name", text: $name)
      Toggle("Enabled", isOn: $enabled)
      Picker("Choice", selection: $choice) {
        Text("One").tag(1)
        Text("Two").tag(2)
      }
    }
    } }

    The example above shows a form with text, toggle, and picker controls bound to @State.



    Segmented Picker & Stepper

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct InputsAdvancedDemo: View {
      @State private var selection = 0
      @State private var count = 1
      var body: some View {
    
    Form {
      Picker("Options", selection: $selection) {
        Text("A").tag(0); Text("B").tag(1); Text("C").tag(2)
      }
      .pickerStyle(.segmented)
      Stepper("Count: \(count)", value: $count, in: 1...5)
    }
    } }
  • SwiftUI Lists & Forms Form

    SwiftUI Lists & Forms: Form

    Group input controls with Form to build settings and data-entry screens.


    Example: Basic Form

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct FormBasicDemo: View {
      @State private var name = ""
      @State private var notifications = true
      var body: some View {
    
    Form {
      Section(header: Text("Profile")) { TextField("Name", text: $name) }
      Section(header: Text("Preferences")) { Toggle("Notifications", isOn: $notifications) }
    }
    } }


    Example: Validation & Submit

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct FormAdvancedDemo: View {
      @State private var email = ""
      @State private var accepted = false
      var valid: Bool { email.contains("@") && accepted }
      var body: some View {
    
    Form {
      Section(header: Text("Account")) {
        TextField("Email", text: $email).keyboardType(.emailAddress)
        Toggle("Accept Terms", isOn: $accepted)
      }
      Section { Button("Submit") { } .disabled(!valid) }
    }
    } }
  • SwiftUI Lists & Forms Section

    Section

    Group rows in lists and inputs in forms using Section with optional headers and footers.


    Example: Sections in List

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SectionListDemo: View {
      var body: some View {
    
    List {
      Section(header: Text("Fruits"), footer: Text("End Fruits")) {
        Text("Apple"); Text("Banana")
      }
      Section(header: Text("Veggies")) { Text("Carrot") }
    }
    } }


    Example: Sections in Form

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SectionFormDemo: View {
      @State private var name = ""
      @State private var notifications = true
      var body: some View {
    
    Form {
      Section(header: Text("Profile"), footer: Text("Shown to others")) {
        TextField("Name", text: $name)
      }
      Section(header: Text("Preferences")) {
        Toggle("Notifications", isOn: $notifications)
      }
    }
    } }
  • SwiftUI Lists & Forms ForEach

    ForEach

    Iterate over collections to generate views dynamically using ForEach.


    ForEach

    Render a list of items using ForEach.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct ForEachDemo: View {
      let nums = [1,2,3]
      var body: some View {
    
    VStack {
      ForEach(nums, id: \.self) { n in
        Text("Item \(n)")
      }
    }
    } }
  • SwiftUI Lists & Forms List

    List

    Render collections with List using identifiable items.


    Basic List

    Render a list of items using List and ForEach.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Item: Identifiable { let id: Int; let title: String }
    
    struct ListBasicDemo: View {
      let items = [Item(id: 1, title: "Milk"), Item(id: 2, title: "Bread")]
      var body: some View { List(items) { Text($0.title) } }
    }

    The example above shows a list of items using List and ForEach.



    Custom rows with ForEach

    Render a list of items using List and ForEach.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Product: Identifiable { let id: Int; let name: String; let price: Double }
    
    struct ListCustomRowsDemo: View {
      let items = [
    
    Product(id: 1, name: "Coffee", price: 2.99),
    Product(id: 2, name: "Tea", price: 1.99)
    ] var body: some View {
    List {
      ForEach(items) { p in
        HStack { Text(p.name); Spacer(); Text(String(format: "$%.2f", p.price)) }
      }
    }
    } }
  • SwiftUI Lists & Forms

    SwiftUI Lists & Forms

    Build data-driven lists with List and collect inputs with Form, rows, and controls.


    Lists

    Render collections with List and ForEach using identifiable items.

    Syntax:

    • List(items) { Text($0.title) }
    • or List { ForEach(items) { ... } }

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Todo: Identifiable { let id: Int; let title: String }
    
    struct TodoList: View {
      let items = [Todo(id: 1, title: "Buy milk"), Todo(id: 2, title: "Walk dog")]
      var body: some View {
    
    List(items) { t in Text(t.title) }
    } }

    This example displays a simple list of identifiable items using List.



    Forms

    Group input controls using Form and built-in rows.

    Syntax: Form { Section { TextField(...) } Toggle(...) Stepper(...) }

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct SettingsForm: View {
      @State private var name = ""
      @State private var notifications = true
      @State private var count = 1
      var body: some View {
    
    Form {
      Section(header: Text("Profile")) {
        TextField("Name", text: $name)
      }
      Section(header: Text("Preferences")) {
        Toggle("Notifications", isOn: $notifications)
        Stepper("Count: \(count)", value: $count, in: 1...10)
      }
    }
    } }
  • SwiftUI Data Flow

    Environment (Values)


    SwiftUI Data Flow @Environment (Values)

    Read system- or app-provided values like color scheme and locale using @Environment.


    Read environment values (color scheme)

    Access the current color scheme from the environment and display it in the UI.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct EnvironmentDemo: View {
      @Environment(\.colorScheme) var scheme
      var body: some View {
    
    Text(scheme == .dark ? "Scheme: Dark" : "Scheme: Light")
      .padding()
    } }

    The example above shows a shared object from an ancestor using .environmentObject and consume it in descendants via @EnvironmentObject.

  • SwiftUI Data Flow @EnvironmentObject

    @EnvironmentObject

    Provide shared app-wide state from an ancestor view and consume it with @EnvironmentObject.


    Share app-wide state with @EnvironmentObject

    Provide a shared object from an ancestor using .environmentObject and consume it in descendants via @EnvironmentObject.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    final class AppSettings: ObservableObject { @Published var theme = "Light" }
    
    struct Root: View {
      @StateObject private var settings = AppSettings()
      var body: some View {
    
    VStack(spacing: 8) {
      Button("Toggle") { settings.theme = (settings.theme == "Light") ? "Dark" : "Light" }
      Child()
    }
    .environmentObject(settings)
    .padding()
    } } struct Child: View { @EnvironmentObject var settings: AppSettings var body: some View { Text("Theme: \(settings.theme)") } }
  • SwiftUI Data Flow @Binding

    @Binding

    Use @Binding to pass a reference to a parent’s state into a child view so updates flow back.


    Bind child input to parent state

    Bind a parent’s state to a child input so edits update the parent value in real time.

    Example

    Demo.swift

    ContentView.swift

    App.swift

    import SwiftUI
    
    struct Child: View {
      @Binding var text: String
      var body: some View {
    
    TextField("Enter", text: $text)
      .textFieldStyle(.roundedBorder)
    } } struct Parent: View { @State private var name = "Kai" var body: some View {
    VStack {
      Text(name)
      Child(text: $name)
    }
    .padding()
    } }