In objective-c I can just say the following.
[self.promoTextField setKeyboardType:UIKeyboardTypeEmailAddress];
I tried googling it but just ge
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
Try it in Swift 3:
let emailTextField: UITextField = {
let text = UITextField()
text.keyboardType = .emailAddress
return text
}()
Try this :
self.promoTextField.keyboardType = UIKeyboardType.emailAddress
self.promoTextField.keyboardType = UIKeyboardType.EmailAddress
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.
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 keyboardType
s 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
}
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
}
}
}