Disable UITextField keyboard?

IphoneCocoa TouchUitextfieldIphone Softkeyboard

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

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 - Iphone

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

textField.inputView = UIView()

does the trick

Solution 3 - Iphone

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 - Iphone

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 - Iphone

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

Solution 6 - Iphone

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

Solution 7 - Iphone

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

Solution 8 - Iphone

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) <<<
}

Solution 9 - Iphone

private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
    {
        ((UITextField)sender).ResignFirstResponder();
    }

In C# this worked for me, I don't use the storyboard.

Solution 10 - Iphone

In Xcode 8.2 you can do it easily by unchecking state "enabled" option.

  1. Click on the textField you want to be uneditable
  2. Go to attirube inspector on right side
  3. Uncheck "enabled" for State

enter image description here

Or if you want to do it via code. You can simply create an @IBOutlet of this text field, give it a name and then use this variable name in the viewDidLoad func (or any custom one if you intent to) like this (in swift 3.0.1):

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    myTextField.isEditable = false
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmichaelView Question on Stackoverflow
Solution 1 - IphoneRohinNZView Answer on Stackoverflow
Solution 2 - IphoneMartin RomañukView Answer on Stackoverflow
Solution 3 - IphoneCalebView Answer on Stackoverflow
Solution 4 - Iphonejcane86View Answer on Stackoverflow
Solution 5 - IphoneDavidView Answer on Stackoverflow
Solution 6 - IphoneNischal RevooruView Answer on Stackoverflow
Solution 7 - IphoneAbdulrazzaq AlzayedView Answer on Stackoverflow
Solution 8 - IphoneLance SamariaView Answer on Stackoverflow
Solution 9 - Iphoneuser5136853View Answer on Stackoverflow
Solution 10 - IphoneNabeel KhanView Answer on Stackoverflow