In SwiftUI, TextField("", text: $input)
has this automatic behavior, when you press done, the keyboard is dismissed. This is exactly what I wanted with the done button.
However, if you use TextField("", text: $input, axis: .vertical)
the done button will become line change instead, and will no longer dismiss the keyboard.
I need scrollable TextField as TextField("", text: $input, axis: .vertical)
. At the same time, pressing done button to dismiss the keyboard instead of line change. Does anyone knows how to achieve it? Thank you!
I think you have to add one button above the keyboard which is called Tool bar button
Hope this will help
struct ContentView: View {
private enum Field: Int, CaseIterable {
case username
}
@State private var username: String = ""
@FocusState private var focusedField: Field?
var body: some View {
NavigationView {
TextField("Username", text: $username)
.focused($focusedField, equals: .username)
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Done") {
focusedField = nil
}
}
}
}
}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With