Have a SwiftUI TextField
with .numberPad
keypad. Want to allow the user to make their changes, then hit 'Done' or 'Cancel' to dismiss the keyboard (there are other choices on the page) before hitting 'Submit'.
The view containing the field is brought up as a modal, using .FullScreenCover.
Here's the code snippet:
@State var currentBid: Float = 0
@FocusState var isInputActive: Bool
...
TextField("Enter Amount", text: Binding(
get: { String(Int(currentBid)) },
set: { currentBid = Float(Int($0) ?? 0) })
).keyboardType(.numberPad)
.focused($isInputActive)
.toolbar(content: {
ToolbarItemGroup(placement: .keyboard, content: {
Button("Cancel") {
print("CANCELLED")
isInputActive = false
}
Spacer()
Button("Done") {
print("DONE")
isInputActive = false
}
})
})
When you tap the text field, the numeric keyboard comes up, but the button bar does not show. This is on iOS 17.6. tested on-device.
The alternative is to use a regular alphanumeric keyboard with an "ENTER" button. But the field is to enter a number and I have to either filter the value or discard it in case they enter bad data.
I checked online, and others indicated wrapping the whole View inside a NavigationStack
might be needed. Tried it and it didn't work.
Any suggestions? Thanks!
@raminf You're correct on the alternative, you could use the Text(_:value:format:prompt:) API and specify a number FormatStyle
The behavior and resulting limitations you describe are by design. Please file an enhancement request using Feedback Assistant If you'd like Apple to consider adding the necessary functionality. Once you file the request, please post the FB number here.