Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dismiss the iOS keyboard?

Tags:

ios

keyboard

I have a UITextfield that i'd like to dismiss the keyboard for. I can't seem to make the keyboard go away no matter what code i use.

like image 552
Tyler McMaster Avatar asked Aug 02 '11 01:08

Tyler McMaster


People also ask

How do I get my iPhone keyboard back to normal?

Go to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access.

How do you dismiss a keyboard from the tap?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do I dismiss my iPad keyboard?

How to hide the keyboard on iPad devices using the Press Key method. This is useful when there are objects behind the keyboard. To automatically close the keyboard on iPad devices, click on the “Hide Keyboard” button (circled below). To hide the keyboard, use the Press Key method with the HIDE_KEYBOARD Key value.


4 Answers

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.

like image 137
benzado Avatar answered Nov 02 '22 10:11

benzado


[myTextField resignFirstResponder]

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

like image 24
Evan Mulawski Avatar answered Nov 02 '22 10:11

Evan Mulawski


I've discovered a case where endEditing and resignFirstResponder fail. This has worked for me in those cases.

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)
like image 27
Andres Canella Avatar answered Nov 02 '22 08:11

Andres Canella


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];
like image 24
user2216794 Avatar answered Nov 02 '22 08:11

user2216794