Disable UITextField keyboard?

65,159

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)

enter image description here

Share:
65,159
michael
Author by

michael

Updated on August 22, 2020

Comments

  • michael
    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
    michael about 13 years
    its 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
    michael about 13 years
    wohoo, got it to work. Thanks for that, something so simple and google didnt have the answer.
  • BreadicalMD
    BreadicalMD almost 11 years
    Caleb's answer below explains this, but I wanted to add that any UIView can be set as the inputView. This will cause the view to animate in and animate out the same way the standard keyboard does.
  • Sebyddd
    Sebyddd almost 10 years
    He asked for UITextField, not UITextView
  • CSolanaM
    CSolanaM over 9 years
    This 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
    CSolanaM over 9 years
    Finally I used a cleaner approach using the appearance proxy, but relying the same concept: [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setInputView:[UIView new]];
  • Yucel Bayram
    Yucel Bayram over 7 years
    If you will do this textfield will be untouchable. So you cannot click and write any stuff.
  • Norolim
    Norolim about 7 years
    This works but if you use Mac keyboard to write in UITextfield the app crashes. Its not a good solution.
  • Pavlos
    Pavlos over 6 years
    Martin and Satish did it again 👌🏻
  • Vitya Shurapov
    Vitya Shurapov over 5 years
    Your 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
    Andrea.Ferrando over 5 years
    isEditable doesn't exist
  • Brian L
    Brian L over 3 years
    You can hide the accessoryView with textField.inputAccessoryView = UIView() and you can hide the cursor with textField.tintColor = .white as well (Swift 5.x).
  • M Hamayun zeb
    M Hamayun zeb about 2 years
    OutStanding working for me......