Forms and Input: TextField, Toggle and Picker
Form
Form makes a grouped settings-style screen, the standard look for input on iOS.
TextField and SecureField
swift
struct SignupView: View {
@State private var name = ""
@State private var email = ""
@State private var password = ""
@State private var subscribe = true
@State private var country = "India"
let countries = ["India", "USA", "UK", "Canada"]
var body: some View {
NavigationStack {
Form {
Section("Account") {
TextField("Full name", text: $name)
.textContentType(.name)
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
SecureField("Password", text: $password)
.textContentType(.newPassword)
}
Section("Preferences") {
Toggle("Send me news", isOn: $subscribe)
Picker("Country", selection: $country) {
ForEach(countries, id: \.self) { Text($0) }
}
}
Section {
Button("Create account") { print("Create \(name)") }
.disabled(!isValid)
}
}
.navigationTitle("Sign up")
}
}
var isValid: Bool {
!name.isEmpty && email.contains("@") && password.count >= 6
}
}
The dollar sign:
$name is a binding. It gives the TextField permission to read and write the name variable. Typing in the field changes the state, and the state change updates the screen.
Picker Styles
swift
Picker("Size", selection: $size) { ... }.pickerStyle(.menu) // dropdown (default in Form)
Picker("Size", selection: $size) { ... }.pickerStyle(.segmented) // tabs
Picker("Size", selection: $size) { ... }.pickerStyle(.wheel) // spinning wheel
Picker("Size", selection: $size) { ... }.pickerStyle(.navigationLink)// new scree
Using an enum for options is the cleanest approach:
swift
enum Size: String, CaseIterable, Identifiable {
case small, medium, large
var id: Self { self }
}
@State private var size: Size = .medium
Picker("Size", selection: $size) {
ForEach(Size.allCases) { Text($0.rawValue.capitalized).tag($0) }
}
.pickerStyle(.segmented)
Multiline Text and Focus
swift
struct NoteEditor: View {
@State private var note = ""
@FocusState private var focused: Bool
var body: some View {
VStack {
TextEditor(text: $note)
.frame(height: 150)
.border(.gray.opacity(0.3))
.focused($focused)
Button("Done") { focused = false } // hide the keyboard
}
.padding()
}
}
Handle the Return Key
swift
@FocusState private var field: Field?
enum Field { case email, password }
TextField("Email", text: $email)
.focused($field, equals: .email)
.submitLabel(.next)
.onSubmit { field = .password }
SecureField("Password", text: $password)
.focused($field, equals: .password)
.submitLabel(.go)
.onSubmit { login() }
Tip: Always set
keyboardType and textContentType so iOS can show the right keyboard and offer AutoFill for emails and passwords.
Conclusion
Form groups input into a clean settings-like screen. Bind each control to state with the dollar sign and validate before enabling the submit button.
Quick Summary: Use Form with TextField, SecureField, Toggle and Picker. Bind with $, use FocusState for the keyboard, and disable buttons until input is valid.

