[ACCEPTED]-Disable UITextField keyboard?-iphone-softkeyboard

Accepted answer
Score: 107

The UITextField's inputView property is 7 nil by default, which means the standard 6 keyboard gets displayed.

If you assign it 5 a custom input view, or just a dummy view 4 then the keyboard will not appear, but the 3 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 2 want to hide both the keyboard and the blinking 1 cursor then use this approach:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;  // Hide both keyboard and blinking cursor.
}
Score: 73

For Swift 2.x, 3.x, 4.x, 5.x

textField.inputView = UIView()

does the trick

0

Score: 5

If it's a UITextField, you can set it's 10 enabled property to NO.

If it's a UITextView, you 9 can implement -textViewShouldBeginEditing: in its delegate to return 8 NO, so that it'll never start editing. Or 7 you can subclass it and override -canBecomeFirstResponder to return 6 NO. Or you could take advantage of its editing 5 behavior and put your numeric buttons into 4 a view which you use as the text view's 3 inputView. This is supposed to cause the buttons 2 to be displayed when the text view is edited. That 1 may or may not be what you want.

Score: 4

Depending on how you have your existing 4 buttons working this could break them, but 3 you could prevent the keyboard from showing 2 up setting the textView's editable property 1 to NO

myTextView.editable = NO
Score: 2

I have the same problem when had 2 textfields 9 on the same view. My purpose was to show 8 a default keyboard for one textfield and 7 hide for second and show instead a dropdown 6 list.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 

method simply did not work as I expected 5 for 2 textfields , the only workaround I 4 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 3 for a target textField (DropDownList in my case) and 2 show a default one when user switches to 1 the 2nd textfield (Account number on my screenshot)

enter image description here

Score: 1

enter image description hereThere is a simple hack to it. Place a empty 5 button (No Text) above the keyboard and 4 have a action Event assign to it. This will 3 stop keyboard coming up and you can perform 2 any action you want in the handle for the 1 button click

Score: 0

To disable UITextField keyboard:

  1. Go to Main.Storyboard
  2. Click on the UITextField to select it
  3. Show the Attributes inspector
  4. Uncheck the User Interaction Enabled

To disable UITextView keyboard:

  1. Go to Main.Storyboard
  2. Click on the UITextView to select it
  3. Show the Attributes inspector
  4. Uncheck the Editable Behavior

0

Score: 0

I used the keyboardWillShow Notification and textField.endEditing(true):

lazy var myTextField: UITextField = {
    let textField = UITextField()
    // ....
    return textField
}()

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}

@objc func keyboardWillShow(_ notification: Notification) {
        
    myTextField.endEditing(true)

    // if using a textView >>> myTextView.endEditing(true) <<<
}

0

More Related questions