48

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pablo
  • 483
  • 1
  • 4
  • 4

6 Answers6

101

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

myTextField.keyboardType = UIKeyboardTypeNumberPad;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
33

This could be done in the Interface Builder as well:

Number pad2 Number pad2

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Luda
  • 7,282
  • 12
  • 79
  • 139
10

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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
6

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

kalpa
  • 884
  • 1
  • 15
  • 22
3

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;
1

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

myTextField.keyboardType = UIKeyboardType.numberPad
dimaskpz
  • 81
  • 8
  • You do not [have to write the parent class](https://stackoverflow.com/questions/3796772/how-can-i-show-the-numeric-keyboard-by-default-on-iphone/70073262#comment100583131_3796784) before the dot. It is in fact recommended to not wrtie it in recent versions of Swift. Just start with the dot and Xcode is supposed to autocomplete properly. – Eric Aya Nov 22 '21 at 22:46