60

In objective-c I can just say the following.

[self.promoTextField setKeyboardType:UIKeyboardTypeEmailAddress];

I tried googling it but just get ways to do it in objective-c.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
user1898829
  • 3,437
  • 6
  • 34
  • 62
  • I recently discovered that you can have the OS suggest an email address like this https://stackoverflow.com/a/61170506/1898829 – user1898829 Apr 14 '20 at 11:08

7 Answers7

152

Try this :

Swift 3

self.promoTextField.keyboardType = UIKeyboardType.emailAddress
// Or Shorter version
self.promoTextField.keyboardType = .emailAddress

Swift < 3

self.promoTextField.keyboardType = UIKeyboardType.EmailAddress
Pintouch
  • 2,630
  • 1
  • 15
  • 22
  • 6
    You can also use the shorthand like this: `self.promoTextField.keyboardType = .EmailAddress` – Clifton Labrum May 05 '15 at 18:31
  • 3
    If keyboardType of currently focussed textField needs to be changed without resigning it from the first responder, you must call reloadInputViews() after changing the keyboardType. Example: self.promoTextField.reloadInputViews() – Vipin Oct 25 '18 at 05:15
  • Does anyone know why setting it the first time works, but for subsequent ones, you have to change `.keyboardType` to something else and then back again? – Eric Apr 22 '21 at 06:32
17

The documentation about UITextInputTraits, a protocol adopted by UITextField, says it's still here:

optional var keyboardType: UIKeyboardType { get set }

And the list of all keyboardTypes is here :

enum UIKeyboardType : Int {
    case Default
    case ASCIICapable
    case NumbersAndPunctuation
    case URL
    case NumberPad
    case PhonePad
    case NamePhonePad
    case EmailAddress
    case DecimalPad
    case Twitter
    case WebSearch
}
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • For more details visit here https://betterprogramming.pub/12-shades-of-keyboard-types-in-ios-a413cf93bf4f – iOS Lifee Apr 29 '22 at 06:25
8

In Swift 3 you might use:

youremailfieldname.keyboardType = UIKeyboardType.emailAddress

Note the lowercase in emailAddress

Note: also that you can assign the keyboard type in the TextField definition in the storyboard.

enter image description here

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
RobDingwall
  • 109
  • 1
  • 3
1

Try it in Swift 3:

let emailTextField: UITextField = {
    let text = UITextField()
    text.keyboardType = .emailAddress

    return text
}()
javimuu
  • 1,829
  • 1
  • 18
  • 29
1

Not sure why but to get the best experience I have to also set a few other properties.

TextField("Email", text: $email)
    .keyboardType(.emailAddress)
    .autocapitalization(UITextAutocapitalizationType.none)
    .disableAutocorrection(true)
Nelu
  • 16,644
  • 10
  • 80
  • 88
0

You should just set the type of entry to TextField you want to change. e.g.

self.yourlTextField.keyboardType = .emailAddress //shows keyboard with e-mail characters

self.yourTextField.keyboardType = .phonePad ////shows phone pad only..just numbers
DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Alan Silva
  • 139
  • 1
  • 8
0

Sometimes give UITextField extension to add one method, you can use like that "textField.chooseKeyboardType(keyboardType:.pad)", perhaps this is not a good way, but useful.

public extension UITextField {

func chooseKeyboardType(_ keyboardType:UIKeyboardType) {
    switch keyboardType {
    case .emailAddress:
        self.keyboardType = .emailAddress
    case .twitter:
        self.keyboardType = .twitter
    case .numberPad:
        self.keyboardType = .numberPad
    case .numbersAndPunctuation:
        self.keyboardType = .numbersAndPunctuation
    case .asciiCapable:
        self.keyboardType = .asciiCapable
    case .URL:
        self.keyboardType = .URL
    case .decimalPad:
        self.keyboardType = .decimalPad
    case .namePhonePad:
        self.keyboardType = .namePhonePad


    default:
        self.keyboardType = .default
    }
}
}
Marc Steven
  • 477
  • 4
  • 16