Input keyboard type - SwiftUI

In SwiftUI, you can set a keyboard type by using the keyboardType view modifier.

The following types are defined:

  • asciiCapable: a keyboard that displays standard ASCII characters
  • asciiCapableNumberPad: a number pad that outputs only ASCII digits
  • decimalPad: a keyboard with numbers and a decimal point
  • default: the default keyboard
  • emailAddress: a keyboard for entering email addresses
  • namePhonePad: a keypad for entering a person’s name or phone number
  • numberPad: a numeric keypad for PIN entry
  • numbersAndPunctuation: a keyboard for numbers and punctuation
  • phonePad: a keypad for entering telephone numbers
  • URL: a keyboard for URL entry
  • twitter: a keyboard for Twitter text entry, with easy access to the at '@' and hash '#' characters
  • webSearch: a keyboard for web search terms and URL entry

Example of using keyboardType:

@State private var phoneNumber: String = ""

var body: some View {
    TextField("Phone Number", text: $phoneNumber)
        // Set keyboard type
        .keyboardType(.numberPad)
}