How do I programmatically switch keyboard layouts

2019-07-05 05:59发布

问题:

I am having trouble finding resources on this, it may be a keyword thing. I am developing for the iPad, and I have text fields with numbers. I understand there is not a dedicated numeric keyboard, I wish to know if there is a way after the keyboard shows to programmatically switch to the numeric view.

I have seen other apps do this, so I don't think it is a matter of 'if' as much as 'how'. How do I programmatically switch keyboard layouts on the iPad?

回答1:

Set your text field's keyboardType property to UIKeyboardTypeNumberPad or UIKeyboardTypeNumbersAndPunctuation. I think on an iPad they will currently both give you the numeric+punctuation keyboard, but perhaps a future version of iOS will have a numeric only keyboard, so you should use request one if it's more appropriate.

If you are creating the text field programatically:

UITextField *numericTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 185, 30)];
numericTextField.adjustsFontSizeToFitWidth = YES;
numericTextField.keyboardType = UIKeyboardTypeNumberPad;
[parentView addSubview:numericTextField];

Or if you're using interface builder, there is a setting in the inspector pane to control the keyboard type.