95

SwiftUI Secure TextField Tutorial

 4 years ago
source link: https://www.tuicool.com/articles/JNzMJfz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A secure textfield displays an editable text area where the entered characters are hidden. In this tutorial a password field will be displayed using a secure textfield. The entered password will be used to instantly update a text view.SwiftUI requires Xcode 11 and MacOS Catalina, which can be downloaded at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project . In the template selector, select iOS as the platform, select the Single View App template, and then click Next . Enter SwiftUISecureTextFieldTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next . Choose a location to save the project on your Mac.

eIBVfqr.png!web

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

QF3Ibe3.png!web

In the Project navigator, click to select ContentView.swift . Change the code inside the ContentView struct to

struct ContentView: View {
    // 1.
    @State private var password: String = ""
    var body: some View {
        Form {
            Section(header: Text("Authentication")) {
                // 2.
                SecureField("Enter a password", text: $password)
                // 3.
                Text("You entered: \(password)")
            }
        }
    }
}
  1. A State property is declared which will represent the entered text inside the textfield

  2. A secure textfield is displayed with a placeholder text.

  3. The entered text will be displayed inside the text view.

Go to the preview pane and select the live view button. Enter a password inside the secure textfield so the text view will be instantly updated.

7rYB73E.png!web

The source code of the SwiftUISecureTextFieldTutorial can be downloaded at the ioscreator repository on Github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK