Skip to content

Instantly share code, notes, and snippets.

@ashishkakkad8
Created March 14, 2022 03:48
Show Gist options
  • Save ashishkakkad8/fe64e4936725ddd60035cc62ed6e89aa to your computer and use it in GitHub Desktop.
Save ashishkakkad8/fe64e4936725ddd60035cc62ed6e89aa to your computer and use it in GitHub Desktop.
How to hide Keyboard in SwiftUI?
private enum Field: Int, CaseIterable {
case username, password
}
@State private var username: String = ""
@State private var password: String = ""
@FocusState private var focusedField: Field?
var body: some View {
NavigationView {
Form {
TextField("Username", text: $username)
.focused($focusedField, equals: .username)
SecureField("Password", text: $password)
.focused($focusedField, equals: .password)
}
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Done") {
focusedField = nil
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment