Pablo
Pablo

Reputation: 483

How can I show the numeric keyboard by default on iPhone?

I'm trying the following with no luck. When the user click on a UiTextfield I need to change the keyboard view to the numeric view automatically, is this possible?

Upvotes: 48

Views: 49108

Answers (6)

dimaskpz
dimaskpz

Reputation: 87

it is a good practice to always write the parent class UIKeyboardType and then continue with the keyboard enum numberPad

myTextField.keyboardType = UIKeyboardType.numberPad

Upvotes: 0

Luda
Luda

Reputation: 7068

This could be done in the Interface Builder as well:

Number pad2 Number pad2

Upvotes: 33

Jacob Relkin
Jacob Relkin

Reputation: 163238

The UITextInputTraits protocol (which UITextField conforms to) has a keyboardType property of type UIKeyboardType.

myTextField.keyboardType = UIKeyboardTypeNumberPad;

Upvotes: 101

kalpa
kalpa

Reputation: 982

We can get it by interface approach also.Select the text field and go to attribute inspector and change the keyboard type from the keyboard drop down box.

enter image description here

Upvotes: 6

If you want to show a numerical keyboard but also need to be able to use decimal points, currency symbols, or other characters, you can have the keyboard default to the back side of the alphabet keyboard.

currentTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

Upvotes: 3

taskinoor
taskinoor

Reputation: 46027

UITextField conforms to UITextInputTraits protocol which means it has keyboardType property which you can set.

myField.keyboardType = UIKeyboardTypeNumberPad;

You can also set it via interface builder. From the text input traits set the keyboard to number pad and that's all.

Upvotes: 10

Related Questions