Disable UITextField keyboard?
Solution 1
The UITextField's inputView property is nil by default, which means the standard keyboard gets displayed.
If you assign it a custom input view, or just a dummy view then the keyboard will not appear, but the blinking cursor will still appear:
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor
If you want to hide both the keyboard and the blinking cursor then use this approach:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide both keyboard and blinking cursor.
}
Solution 2
For Swift 2.x, 3.x, 4.x, 5.x
textField.inputView = UIView()
does the trick
Solution 3
If it's a UITextField, you can set it's enabled
property to NO.
If it's a UITextView, you can implement -textViewShouldBeginEditing:
in its delegate to return NO, so that it'll never start editing. Or you can subclass it and override -canBecomeFirstResponder
to return NO. Or you could take advantage of its editing behavior and put your numeric buttons into a view which you use as the text view's inputView
. This is supposed to cause the buttons to be displayed when the text view is edited. That may or may not be what you want.
Solution 4
Depending on how you have your existing buttons working this could break them, but you could prevent the keyboard from showing up setting the textView's editable property to NO
myTextView.editable = NO
Solution 5
I have the same problem when had 2 textfields on the same view. My purpose was to show a default keyboard for one textfield and hide for second and show instead a dropdown list.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
method simply did not work as I expected for 2 textfields , the only workaround I found was
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTextField.inputView = dummyView;
myTextField.inputAccessoryView = dummyView;
myTextField.tintColor = myTextField.backgroundColor; //to hide a blinking cursor
This will totally hide the keyboard for a target textField (DropDownList in my case) and show a default one when user switches to the 2nd textfield (Account number on my screenshot)
michael
Updated on August 22, 2020Comments
-
michael over 3 years
I put a numeric keypad in my app for inputing numbers into a text view, but in order to input numbers I have to click on the text view. Once I do so, the regular keyboard comes up, which I don't want.
How can I disable the keyboard altogether? Any help is greatly appreciated.
-
michael about 13 yearsits a text field btw. Well this is how i getting the buttons to write in the specific textfield, setting flags and calling if (current.tag == 10) { textfieldflag = 1; [loanAmount setUserInteractionEnabled:NO]; [period setUserInteractionEnabled:YES]; [interest setUserInteractionEnabled:YES]; //[deposit setUserInteractionEnabled:YES]; loanAmount.editable = NO; }
-
michael about 13 yearswohoo, got it to work. Thanks for that, something so simple and google didnt have the answer.
-
BreadicalMD almost 11 yearsCaleb's answer below explains this, but I wanted to add that any
UIView
can be set as theinputView
. This will cause the view to animate in and animate out the same way the standard keyboard does. -
Sebyddd almost 10 yearsHe asked for UITextField, not UITextView
-
CSolanaM over 9 yearsThis is useful when developing a custom keyboard app extension. If you include a UISearchBar or a UITextField in the custom keyboard, it will try to show the keyboard (although it IS IN a keyboard), so that means it will kill your current keyboard as of its views / controllers hierarchy and then recreate it. I solved it with the help with this old-school hack. Thank you!
-
CSolanaM over 9 yearsFinally I used a cleaner approach using the appearance proxy, but relying the same concept: [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setInputView:[UIView new]];
-
Yucel Bayram over 7 yearsIf you will do this textfield will be untouchable. So you cannot click and write any stuff.
-
Norolim about 7 yearsThis works but if you use Mac keyboard to write in UITextfield the app crashes. Its not a good solution.
-
Pavlos over 6 yearsMartin and Satish did it again 👌🏻
-
Vitya Shurapov over 5 yearsYour answer is really great! Because it has the solution with setting inputAccessoryView to dummyView. I was struggling- how to hide it from the bottom of the screen.
-
Andrea.Ferrando over 5 yearsisEditable doesn't exist
-
Brian L over 3 yearsYou can hide the accessoryView with
textField.inputAccessoryView = UIView()
and you can hide the cursor withtextField.tintColor = .white
as well (Swift 5.x). -
M Hamayun zeb about 2 yearsOutStanding working for me......