2

The new SwiftUI is fantastic to play with... I'm trying to use Forms instead of Eureka. A couple of questions:

What is the best way to let the user enter a number? I used to do that with a UIPickerView, see image UIPickerView for numbers.

With SwiftUI I only found Textfield, as in the following code:

import SwiftUI

struct SettingsView : View {
    @State var email = ""
    @State var amount = ""
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Email")) {
                    TextField("Your email", text: $email)
                        .textFieldStyle(.roundedBorder)
                }
                Section(header: Text("Amount")) {
                    TextField("Amount", text: $amount)
                        .textFieldStyle(.roundedBorder)
                }
                    .navigationBarTitle("Settings")
            }
        }
    }
}

When you click in the field, the ABC keyboard comes up. The user can select '123' to get the number keyboard. But I would like to see a number pad instead.

Also, the keyboard blocks the view (if you have more fields); the view doesn't scroll up to make room for the keyboard.

Is it possible to get rid of the keyboard when the user clicks outside a TextField?

And is there a way to 'validate the entries'? For instance, the amount should be between 10 and 1.000?

arakweker
  • 1,535
  • 4
  • 18
  • 40
  • 1
    You should probably separate each of these out into completely separate questions. 1. How do I set the keyboard input to number pad for TextField? 2. Keyboard blocks TextField in List. 3. Dismiss keyboard on tap outside TextField. 4. Validate entries in TextField. – graycampbell Jul 17 '19 at 00:16
  • 1
    I say this because 1 and 2 are similar to questions that have already been asked [here](https://stackoverflow.com/questions/56517515/how-to-set-keyboard-type-of-textfield-in-swiftui/56745176#56745176) and [here](https://stackoverflow.com/questions/56491881/move-textfield-up-when-thekeyboard-has-appeared-by-using-swiftui-ios/56721268#56721268), but 3 and 4 could be useful questions if asked individually. – graycampbell Jul 17 '19 at 00:22

0 Answers0