Created
October 30, 2021 10:29
-
-
Save disc0infern0/cbfa6e8f0ca0c1acfeda8b0e384f7c6e to your computer and use it in GitHub Desktop.
Simple Login form to demonstrate change of focus in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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