Programmatically change UITextField Keyboard type Programmatically change UITextField Keyboard type ios ios

Programmatically change UITextField Keyboard type


There is a keyboardType property for a UITextField:

typedef enum {    UIKeyboardTypeDefault,                // Default type for the current input method.    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).    UIKeyboardTypeDecimalPad,             // A number pad including a decimal point    UIKeyboardTypeTwitter,                // Optimized for entering Twitter messages (shows # and @)    UIKeyboardTypeWebSearch,              // Optimized for URL and search term entry (shows space and .)    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated} UIKeyboardType;

Your code should read

if(user is prompted for numeric input only)    [textField setKeyboardType:UIKeyboardTypeNumberPad];if(user is prompted for alphanumeric input)    [textField setKeyboardType:UIKeyboardTypeDefault];


It's worth noting that if you want a currently-focused field to update the keyboard type immediately, there's one extra step:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress[textField setKeyboardType:UIKeyboardTypeEmailAddress];[textField reloadInputViews];

Without the call to reloadInputViews, the keyboard will not change until the selected field (the first responder) loses and regains focus.

A full list of the UIKeyboardType values can be found here, or:

typedef enum : NSInteger {    UIKeyboardTypeDefault,    UIKeyboardTypeASCIICapable,    UIKeyboardTypeNumbersAndPunctuation,    UIKeyboardTypeURL,    UIKeyboardTypeNumberPad,    UIKeyboardTypePhonePad,    UIKeyboardTypeNamePhonePad,    UIKeyboardTypeEmailAddress,    UIKeyboardTypeDecimalPad,    UIKeyboardTypeTwitter,    UIKeyboardTypeWebSearch,    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable} UIKeyboardType;


Yes you can, for example:

[textField setKeyboardType:UIKeyboardTypeNumberPad];