How to change UITextField keyboard type to email in Swift

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.


Try this :

Swift 3

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

Swift < 3

self.promoTextField.keyboardType = UIKeyboardType.EmailAddress

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
}

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