How to disable iOS keyboard?

I have a UITextfield for which I would like to cancel the keyboard. I can't get the keyboard to go away, no matter what code I use.

+74
ios keyboard
Aug 02 2018-11-11T00:
source share
17 answers
[myTextField resignFirstResponder] 

Here is the second paragraph in the section "Showing and hiding the keyboard."

+64
Aug 02 2018-11-11T00:
source share

If you have several text fields and don’t know which one is the first responder (or you simply don’t have access to the text fields from anywhere you write this code), you can call endEditing: containing the text fields in the parent view.

In view controller mode, it will look like this:

 [self.view endEditing:YES]; 

The parameter causes the text field to reset the status of the first responder. If you used a delegate to perform the check and wanted to stop everything until the contents of the text field are valid, you can also encode it as follows:

 BOOL didEndEditing = [self.view endEditing:NO]; if (didEndEditing) { // on to the next thing... } else { // text field must have said to first responder status: "never wanna give you up, never wanna let you down" } 

The endEditing: method endEditing: much better than telling individual resignFirstResponder text fields, but for some reason I didn’t even know about it until recently.

+191
Aug 2 2018-11-21T00:
source share

There are cases when the text field is not the first responder , but the keyboard is on the screen. In these cases, the above methods cannot cancel the keyboard.

One example of how to get there:

  • programmatically click ABPersonViewController on the screen; open any contact;
  • tap the note field (which becomes the first responder and launches the keyboard);
  • swipe left in any other field so that the Delete button appears;
  • at this point, you do not have the first responder among the text fields (just check programmatically), but the keyboard is still there. Calling [view endEditing: YES] does nothing.

In this case, you also need to ask the view controller to exit edit mode:

 [viewController setEditing:NO animated:YES]; 
+8
May 03 '13 at 16:35
source share

I suggest you add an action to the header file:

 -(IBAction)removeKeyboard; 

And in the implementation, write something like this:

 -(IBAction)removeKeyboard { [self.textfield resignFirstResponder]; } 

In the NIB file, connect from UITextFiled to the file owner in the DidEndOnExit option. Thus, when you press the return key, the keyboard will disappear.

Hope this helps!

+6
Aug 2 2018-11-11T00:
source share

In your view of the YourViewController.h file controller, make sure that you implement the UITextFieldDelegate protocol:

 @interface YourViewController : <UITextFieldDelegate> @end 

Then, in the YourViewController.m file, the following instance method is implemented:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.yourTextField1 resignFirstResponder]; [self.yourTextField2 resignFirstResponder]; ... [self.yourTextFieldn resignFirstResponder]; } 
+6
Jan 31 '13 at 18:54
source share

I discovered a case where endEditing and resignFirstResponder fail. It worked for me on those occasions.

Objc

 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; [self setEditing:NO]; 

swift

 UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil) 
+6
Apr 28 '15 at 19:30
source share

as a last resort 💩

 let dummyTextView = UITextView(frame: .zero) view.addSubView(dummyTextView) dummyTextView.becomeFirstResponder() dummyTextView.resignFirstResponder() 
+3
May 31 '18 at 8:58
source share

If you do not know which textField is the first responder, you can find it. I am using this function:

 UIView *resignFirstResponder(UIView *theView) { if([theView isFirstResponder]) { [theView resignFirstResponder]; return theView; } for(UIView *subview in theView.subviews) { UIView *result = resignFirstResponder(subview); if(result) return result; } return nil; } 

Then in your code call:

  UIView *resigned = resignFirstResponder([UIScreen mainScreen]); 
+2
Oct 17 '12 at 0:20
source share

This will cancel a single text box.

 // Swift TextField.resignFirstResponder() // Objective C [TextField resignFirstResponder]; 

To cancel any text field, use the code below

 // Swift self.view!.endEditing(true) // Objective C [self.view endEditing:YES]; 
+2
Jan 06 '16 at 12:36
source share

To resign any text field in the application

 UIApplication.shared.keyWindow?.endEditing(true) 

This approach is clean and guaranteed to work, because keyWindow is, by definition, the root representation of all possible representations that display the keyboard ( source ):

The key window receives keyboard and other non-touch events. Only one window at a time can be a key window.

+2
May 14 '18 at 5:40
source share

You just replace yourTextFieldName , you guessed it! your text box. This will close the keyboard.

 [yourTextFieldName resignFirstResponder]; 
+1
Jan 31 '12 at 15:34
source share
 -(void)methodName { [textFieldName resignFirstResponder]; } 

call this method (methodName) with didEndOnExit

+1
Feb 22 '12 at 6:10
source share

For Swift 3

You can hide the keyboard as follows:

 textField.resignFirstResponder() 

If you want to hide the keyboard when the user presses the "intro" button, you must implement the following UITextFieldDelegate method:

 func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } 
+1
Jan 17 '17 at 15:50
source share
 -(void)textFieldDidBeginEditing:(UITextField *)textField { // your code [textField reloadInputViews]; } 
0
Apr 19 '16 at 20:39
source share

3 quick and easy steps

Add a UITextFieldDelegate to your class, like a UITextFieldDelegate below:

 class RegisterVC: UIViewController, UITextFieldDelegate { //class implementation } 

in the class implementation add the delegate function textFieldShouldEndEditing: ::

 internal func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.view.endEditing(true) return true } 

and as a last step, set your UITextField UITextField to yourself, somewhere suitable. For example, inside the viewDidLoad function:

 override func viewDidLoad(){ super.viewDidLoad() myTextField1.delegate = self myTextField2.delegate = self .. .. } 

Now, when the user presses the return key, the keyboard will be rejected.

I also prepared a code snippet, you can check it here .

0
Apr 20 '18 at 18:24
source share

Swift 4.2 and it worked for me

 import UIKit class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var textField: UITextField! override func viewDidLoad() { super.viewDidLoad() self.textField.delegate = self } // hide key board when the user touches outside keyboard override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) } // user presses return key func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } 

}

0
Jul 09 '19 at 3:35
source share
  • Configure the Finished Exit event in Xcode (right-click on the text field).

    Screenshot

  • Implement this method:

     -(IBAction) closeKeyboard:(id) sender { [_txtField resignFirstResponder]; } 
-one
Oct 13 '16 at 21:53
source share



All Articles