struct SimpleFocusState: View { @State private var username = "" @State private var password = "" @FocusState private var isUsernameFocused: Bool @FocusState private var isPasswordFocused: Bool var body: some View { Form { Section(header: Text("Sign In")) { TextField("Username", text: $username) .focused($isUsernameFocused) .onChange(of: isUsernameFocused) { newUsernameState in if !newUsernameState { checkUsernameAvailability(username: username) } } SecureField("Password", text: $password) .focused($isPasswordFocused) .onChange(of: isPasswordFocused) { newPasswordState in if !newPasswordState { checkPasswordIsValid(password: password) } } HStack { Button("Sign In"){ } Spacer() Button("Cancel") { } } } } } private func checkUsernameAvailability(username: String) { print("\(username) available? \(Bool.random())") } private func checkPasswordIsValid(password: String) { if password.count > 8 { print("Password is valid") } else { print("Try better password please.") } } }
struct FocusOnSignup: View { enum Field: Hashable { case name case username case password case confimPassword } @State private var name = "" @State private var username = "" @State private var password = "" @State private var confimPassword = "" @FocusState private var focusedField: Field? var body: some View { Form { TextField("Name", text: $name) .focused($focusedField, equals: .name) .submitLabel(.next) TextField("Username", text: $username) .focused($focusedField, equals: .username) .submitLabel(.next) TextField("password", text: $password) .focused($focusedField, equals: .password) .submitLabel(.next) TextField("Confim Password", text: $confimPassword) .focused($focusedField, equals: .confimPassword) .submitLabel(.next) } .onSubmit { if focusedField == .name { focusedField = .username } else if focusedField == .username { focusedField = .password } else if focusedField == .password { focusedField = .confimPassword } else if focusedField == .confimPassword { signUp() } } } private func signUp() { print(name, username, password, confimPassword) }}
enum Field: Hashable { case name case username case password case confimPassword }
@State private var name = ""@State private var username = ""@State private var password = ""@State private var confimPassword = ""
@FocusState private var focusedField: Field?
Form { TextField("Name", text: $name) .focused($focusedField, equals: .name) .submitLabel(.next) TextField("Username", text: $username) .focused($focusedField, equals: .username) .submitLabel(.next) TextField("password", text: $password) .focused($focusedField, equals: .password) .submitLabel(.next) TextField("Confim Password", text: $confimPassword) .focused($focusedField, equals: .confimPassword) .submitLabel(.next) }
Form { TextField("Name", text: $name) .focused($focusedField, equals: .name) .submitLabel(.next) TextField("Username", text: $username) .focused($focusedField, equals: .password) .submitLabel(.next) TextField("password", text: $password) .focused($focusedField, equals: .username) .submitLabel(.next) TextField("Confim Password", text: $confimPassword) .focused($focusedField, equals: .confimPassword) .submitLabel(.next) }
.onSubmit { if focusedField == .name { focusedField = .username } else if focusedField == .username { focusedField = .password } else if focusedField == .password { focusedField = .confimPassword } else if focusedField == .confimPassword { signUp() } }
struct DismissKeyboardExample: View { @FocusState private var isPhonePadOpen: Bool @State private var phoneNumber = "" var body: some View { Form { TextField("Phone number", text: $phoneNumber) .focused($isPhonePadOpen) .keyboardType(.phonePad) Button("Close") { isPhonePadOpen = false } } } }