Skip to content

Instantly share code, notes, and snippets.

@disc0infern0
Created October 30, 2021 10:29
Show Gist options
  • Save disc0infern0/cbfa6e8f0ca0c1acfeda8b0e384f7c6e to your computer and use it in GitHub Desktop.
Save disc0infern0/cbfa6e8f0ca0c1acfeda8b0e384f7c6e to your computer and use it in GitHub Desktop.
Simple Login form to demonstrate change of focus in SwiftUI
struct Login: View {
@State private var username = ""
@State private var password = ""
enum Field: Hashable {
case username
case password
}
@FocusState private var focusedField: Field?
var body: some View {
Form {
TextField("Username", text: $username)
.focused($focusedField, equals: .username)
SecureField("Password", text: $password)
.focused($focusedField, equals: .password)
Button("Sign In") {
if username.isEmpty {
focusedField = .username
} else if password.isEmpty {
focusedField = .password
} else {
self.focusedField = nil
// handle login
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment