iOS force App to use custom keyboard

2019-08-07 07:13发布

问题:

I am working on the app which has a one text-field which should accept only numbers so I created one custom keyboard which has only [0-9] as input to accept.To use this custom keyboard one has to go setting-->keyboards then accept this and then open particular keyboard from the app.

Is this possible to force user to open only custom keyboard without going into setting option. I want whenever user opens the app.. only custom keyboard should open, not other

回答1:

Just make it numeric by default. You could also do that from interface builder.

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

Source: How do I programmatically switch keyboard layouts on the iPad?

EDIT: Or your could use the NSNotificationCenter to inform you whether a keyboard is called and simply call your own instead:

Try something like this, didn't try myself though:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];


回答2:

Hi you can this using this code just make a view of your custom keyboard and add this line to show your keyboard instead of default keyboard

numericTextField.inputView = your_keyboardView;//here your_keyboardView is the object of the custom keyboard view. 

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

If you have any query then please let me know.