This simple TextField
might be part of a chat feature, and I would like to be able to send chat messages when I press the keyboard button "send".
(Imagine in this chat I don’t need to allow users to enter newline, by overriding the return key, to be send with the submitLabel(.send)
view modifier.)
TextField(
"Chat...",
text: $draft
)
.submitLabel(.send)
.onSubmit {
if !draft.isEmpty {
sendMessage(draft: draft)
}
}
However, this will hide the keyboard, and I would like to know:
is there any way to prevent the keyboard from hiding when I press send
??
I know how to refocus the field, I can do that with @FocusState
but that still results in a hide keyboard animation starting which then aborts, so looks glithy.
2
Answers
It is possible to prevent keyboard hiding using a somewhat "hacky" solution, combing re-focus of field together with disable animation.
on ios16 the keyboard bounces back everytime when you click on send.