Is it possible to add a .submitLable() to a TextField that has the keyboard type .numberPad. All the information I can find is that it's not possible but that information is 2 years old. So I hope that it might have changed.
I have the following code but the submit label doesn't appear on the keyboard when I actually run the app.
TextField("placeholder", text: $placeholder)
.submitLabel(.next)
.keyboardType(.numberPad)
Is there a way to add a submit label or something of similar functionality to a TextField with a keyboard type of numberPad in SwiftUI or is it just not possible with SwiftUI currently?
The issue is that
.submitLabel()modifies the "return" key display/behavior in the keyboard, but with.numberPadthere is no such key to modify. (You can see this behavior by experimenting with different values for.submitLabel()using the default keyboard type)It is possible to add an
inputAccessoryViewto aUITextFieldwith a system defined and pre-localized.donebutton. (Or.next, or several others) It is somewhat cumbersome, though.For example, using a generic type
Valuethat conforms toBinaryInteger:Here,
UIApplication.dismissKeyboardis an extension onUIApplicationlike this, which essentially tells whatever currently has focus (to show the keyboard) to give it up:I have used this with Swift 5.5 and a target of iOS 16.x. It should work with some prior versions of Swift / iOS, but I have not tested it with anything else.