How to change UITextField keyboard type to email in Swift

SwiftUitextfield

Swift Problem Overview


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.

Swift Solutions


Solution 1 - Swift

Try this :

Swift 3

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

Swift < 3

self.promoTextField.keyboardType = UIKeyboardType.EmailAddress

Solution 2 - Swift

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
}

Solution 3 - Swift

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

Solution 4 - Swift

Try it in Swift 3:

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

Solution 5 - Swift

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
    

Solution 6 - Swift

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
    }
}
}

Solution 7 - Swift

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1898829View Question on Stackoverflow
Solution 1 - SwiftPintouchView Answer on Stackoverflow
Solution 2 - SwiftCyrilleView Answer on Stackoverflow
Solution 3 - SwiftRobDingwallView Answer on Stackoverflow
Solution 4 - SwiftjavimuuView Answer on Stackoverflow
Solution 5 - SwiftAlan SilvaView Answer on Stackoverflow
Solution 6 - SwiftMarc StevenView Answer on Stackoverflow
Solution 7 - SwiftNeluView Answer on Stackoverflow