Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PetreVane/4bd1cd1eabab15249a76052fc206bb81 to your computer and use it in GitHub Desktop.
Save PetreVane/4bd1cd1eabab15249a76052fc206bb81 to your computer and use it in GitHub Desktop.
This is a countinuation of "Form embedded in a NavigationView" code snippet. This screen contains now 4 different Sections, each with a different component.
import SwiftUI
struct ContentView: View {
@State private var pickerSelectedIndex = 0
@State private var toggleOn = false
@State private var textFieldValue = ""
var treats = Treat.demoTreats
var body: some View {
// Navigation
NavigationView {
//Form to collect data
Form {
// Section with Picker presented in a different window
Section {
Picker(selection: $pickerSelectedIndex, label: Text("Treats")) {
ForEach(0 ..< self.treats.count) {
Text(self.treats[$0].name).tag($0)
}
}
}
// Section with Toggle (in the same window)
Section {
Toggle(isOn: $toggleOn) {
Text("Would you like to add some milk? ")
}
if toggleOn {
Text("Milk costs extra 0.99 Eur")
}
}
// Section with TextField
Section {
TextField("Insert your comments here", text: $textFieldValue)
}
// Section with Button
Section {
Button(action: {
print("Your order is being processed ..")
}) {
HStack(alignment: .center, spacing: 10) {
Image(systemName: "paperplane")
.foregroundColor(.green)
.scaledToFit()
Text("Confirm order")
.foregroundColor(.green)
}
}
}
}.navigationBarTitle(Text("Treats for your cat"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment