跳转至
Structure

SecureField

A text field for entering private text, usually a password.

Declaration

struct SecureField<Label> : View where Label : View

Overview

A secure field is just like a TextField, except the entered text is shown as dots instead of as the actual text. Typically, you use this for entering passwords and other sensitive information.

A SecureField uses a binding to a string value, and a closure that executes when the user commits their edits, such as by pressing the Return key. The field updates the bound string on every keystroke or other edit, so you can read its value at any time from another control, such as a Done button.

The following example shows a SecureField bound to the string password. If the user commits their edit in the secure field, the onCommit closure displays a message to confirm that a password has been entered.

struct PasswordView: View {
    @State private var password = ""
    @State private var message = ""

    var body: some View {
        VStack(spacing: 20) {
            Text(message)
            SecureField("Password", text: $password, onCommit: {
                message = "Password has been entered ✅"
            })
        }
    }
}
A gif displaying a secure text field being used, in which each character typed is displayed as a dot on the screen. Once the user confirms the password, a text view appears that reads "Password has been entered."

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Initializer

init(_:text:onCommit:) Creates a text field for entering private text, usually a password, with a LocalizedStringKey placeholder.

init(_:text:onCommit:) Creates a text field for entering private text, usually a password, with a String placeholder.


Type Alias

Body The type of view representing the body of this view.


Instance Property

body The body of a secure field, which is the underlying TextField.