In SwiftUI, how do you configure TextField and SecureField for a checkout sign-in's email and password so the keyboard and AutoFill fit each field?
answer
- two views, one hides its text
- prompt versus label on iOS
- keyboard layout is not validation
- content type feeds AutoFill
- shift key and autocorrect are separate
basics
~10 sBind a TextField and a SecureField to String state, then describe each field: keyboardType(.emailAddress), textContentType(.username), textInputAutocapitalization(.never) and autocorrectionDisabled() on the email, and textContentType(.password) on the SecureField so AutoFill can help.
solid answer
~40 sBoth views take a `Binding<String>` and write it on every keystroke, so the screen's `@State` always holds the current text. Give each a label and a `prompt:`; in an iOS `Form` the prompt is the placeholder and the label is not drawn. The email `TextField` gets `.keyboardType(.emailAddress)` for the layout, `.textContentType(.username)` so the system can offer the saved account, `.textInputAutocapitalization(.never)` and `.autocorrectionDisabled()`. The `SecureField` gets `.textContentType(.password)`; it shows a dot per character, hides them in screenshots, blocks cut and copy, and shows a Caps Lock indicator. A keyboard type only picks the keyboard, never validates what is typed or pasted, so you still validate before submitting.
code
swift · 23 linesimport SwiftUI
struct AccountStep: View {
@State private var email = ""
@State private var password = ""
var body: some View {
Form {
TextField("Email", text: $email, prompt: Text("[email protected]"))
.keyboardType(.emailAddress)
.textContentType(.username)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
SecureField("Password", text: $password, prompt: Text("Required"))
.textContentType(.password)
.submitLabel(.go)
.onSubmit { signIn() }
}
}
private func signIn() { /* validate, then call the account service */ }
}go deeper
Recall the two views, that both bind to a String updated per keystroke, and the four modifiers an email field needs: keyboard type, content type, no autocapitalization, no autocorrection.
Explain how label and prompt are shown in an iOS form, what SecureField adds over TextField, and why the keyboard type never replaces validating the string.
Show the review eye: flag the to-be-deprecated autocapitalization(:) and disableAutocorrection(:) spellings, a missing content type that breaks AutoFill, and a password string that reaches a log.
Frame the field configuration as a shared team convention, such as a small set of field modifiers every sign-in and checkout form reuses, so AutoFill and keyboards behave the same across features.
## Two text-entry views, one binding model A **`TextField`** is SwiftUI's single-line text-entry control, and a **`SecureField`** is its counterpart for private text such as passwords. Both take a `Binding<String>` to a value the screen owns, usually a `@State` property, and both write that string **on every keystroke or other edit**. That matters for the sign-in step of a checkout: anything that reads the email string, such as a validation message, sees the current text while the user is still typing. A `TextField` can also bind to a non-string value through a format style, but that variant behaves differently (it writes only when the edit is committed) and is not what you want for an email address or a password. ## Labels and prompts on iOS Each initializer takes a **label** and, optionally, a **`prompt:`** (a `Text`). The style decides how to show them: - In an iOS `Form`, the prompt becomes the **placeholder** inside the field and the label is not displayed. - If you omit the prompt, the iOS form shows the **label as the placeholder** instead. - On macOS the same form puts the label at the leading edge and uses the prompt as placeholder text. So on iOS the label still earns its place: it names the field even where it is not drawn, and it is the placeholder when there is no prompt. ## Describing each field to the system The keyboard and AutoFill know nothing about your field until you tell them. These modifiers do that: | Modifier | What it controls | Email field | Password field | |---|---|---|---| | `keyboardType(_:)` | Which software keyboard layout appears (a `UIKeyboardType`) | `.emailAddress` | default | | `textContentType(_:)` | What the text means, so the system can offer suggestions and AutoFill | `.username` | `.password` | | `textInputAutocapitalization(_:)` | When the shift key turns on by itself (default `.sentences`) | `.never` | not needed | | `autocorrectionDisabled(_:)` | Whether autocorrection rewrites input | disabled | not needed | | `submitLabel(_:)` | The title on the Return key | `.next` | `.go` | Two of these have older spellings. `autocapitalization(_:)` takes a UIKit `UITextAutocapitalizationType` and the iOS 27 SDK marks it for deprecation with the message "use textInputAutocapitalization(_:)", which arrived in iOS 15. `disableAutocorrection(_:)` is likewise marked for deprecation and renamed to `autocorrectionDisabled(_:)`. ## What SecureField adds Apple's documentation lists what a `SecureField` does beyond a `TextField`: 1. It displays **one dot per character** instead of the text. 2. It **hides the dots when someone takes a screenshot** on iOS. 3. It **prevents cutting or copying** the contents. 4. It shows an indicator when **Caps Lock** is on. It is a separate view type, not a style applied to `TextField`: there is no modifier that turns a `TextField` secure. Its binding still holds the real password string, so treat that state like any other secret and never log it. ## What these modifiers do not do - **A keyboard type is not validation.** `.emailAddress` shows a layout with `@` and `.` at hand, but the user can still paste, dictate or type anything on a hardware keyboard. Validate the string before you submit it. - **Autocorrection and autocapitalization are independent.** Turning off autocorrection does not stop the shift key from capitalizing the first letter; that is `textInputAutocapitalization(.never)`. - **A content type changes no layout.** `textContentType(_:)` describes meaning; the keyboard layout still comes from `keyboardType(_:)`. ## A worked example ```swift import SwiftUI struct AccountStep: View { @State private var email = "" @State private var password = "" var body: some View { Form { TextField("Email", text: $email, prompt: Text("[email protected]")) .keyboardType(.emailAddress) .textContentType(.username) .textInputAutocapitalization(.never) .autocorrectionDisabled() SecureField("Password", text: $password, prompt: Text("Required")) .textContentType(.password) .submitLabel(.go) .onSubmit { signIn() } } } private func signIn() { /* validate, then call the account service */ } } ``` `onSubmit` runs when the user presses Return while the password field has focus; `submitLabel(.go)` only changes the title on that key. Moving the cursor from the email field to the password field with the Return key is a job for `@FocusState`, which is a separate step of building the form.
- When would you use .textContentType(.newPassword) instead of .password?On a create-account step. `.newPassword` says the field is for a password that does not exist yet, so the system's password AutoFill can suggest a generated strong password instead of offering a saved one. Signing in to an existing account keeps `.password`. SwiftUI only passes the content type through; the suggestion itself comes from the system.
- How do you send the sign-in when the user presses Return in the SecureField?Attach `onSubmit` to the field (or to a container around it). Apple's documentation says a `TextField` or `SecureField` triggers it when the user presses the hardware or software Return key. Pair it with `.submitLabel(.go)` or `.done` so the key's title matches the action; the label changes only the title, not what happens.
- keyboardType(.emailAddress) guarantees the field only ever holds a valid email address
- SecureField is a TextField style, so any TextField can be made secure with a modifier
- A String-bound TextField only updates its binding when the user presses Return
- autocorrectionDisabled() also stops the shift key from capitalizing the first letter
- On iOS the label always shows beside the field, so the prompt is redundant