How do I dismiss the iOS keyboard? How do I dismiss the iOS keyboard? ios ios

How do I dismiss the iOS keyboard?


If you have multiple text fields and don't know which one is first responder (or you simply don't have access to the text fields from wherever you are writing this code) you can call endEditing: on the parent view containing the text fields.

In a view controller's method, it would look like this:

[self.view endEditing:YES];

The parameter forces the text field to resign first responder status. If you were using a delegate to perform validation and wanted to stop everything until the text field's contents were valid, you could also code it like this:

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 is much better than telling individual text fields to resignFirstResponder, but for some reason I never even found out about it until recently.


[myTextField resignFirstResponder]

Here, second paragraph in the Showing and Hiding the Keyboard section.


There are cases where no text field is the first responder but the keyboard is on screen.In these cases, the above methods fail to dismiss the keyboard.

One example of how to get there:

  1. push the ABPersonViewController on screen programmatically; open any contact;
  2. touch the "note" field (which becomes first responder and fires up the keyboard);
  3. swipe left on any other field to make the "delete" button appear;
  4. by this point you have no 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 the editing mode:

[viewController setEditing:NO animated:YES];