thary
thary

Reputation: 221

Numbers Keyboard in Xcode

Does any one know how to show the numbers keyboard in the iphone rather than the default one? And is there a way to filter the textfield for numbers only? I need to do that from the code not from interface builder

Thanks

Upvotes: 12

Views: 31697

Answers (5)

Zat42
Zat42

Reputation: 2911

For Swift2

keyboardType is waiting a UIKeyboardType

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
static var Alphabet: UIKeyboardType { get }
}

So it should be something like this :

textField.keyboardType = UIKeyboardType.NumbersAndPunctuation

Apple Documentation - UIKeyboardType

Upvotes: 0

gware
gware

Reputation: 91

There's a keyboard with a decimal included now, you can use the following code.

textField.keyboardType = UIKeyboardTypeDecimalPad;

Upvotes: 9

Shaggy Frog
Shaggy Frog

Reputation: 27601

To add to Kristopher Johnson's answer, UITextField supports the UITextInputTraits protocol, which has a UIKeyboardType @property called keyboardType. You just set that to be whatever you want (in your case, UIKeyboardTypeNumberPad). One place to put this in your code is in the viewDidLoad method of your view controller.

textField.keyboardType = UIKeyboardTypeNumberPad;

Upvotes: 1

Kristopher Johnson
Kristopher Johnson

Reputation: 82535

Set the keyboardType property of the entry field to be numeric, either in Interface Builder or via code.

Something like this should work:

textField.keyboardType = UIKeyboardTypeNumberPad;

For all the possible keyboard types see the Apple docs

Upvotes: 2

John Wang
John Wang

Reputation: 6126

alternatively, set the textfield's keyboardType property in code:

textField.keyboardType = UIKeyboardTypeNumberPad

Available options are:

UIKeyboardType The type of keyboard to display for a given text-based view.

typedef enum {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;

A word of caution though, The NumberPad doesn't have a decimal point for entry. If you need that you'll have to use something else.

Upvotes: 31

Related Questions