By Seventoes

2009-04-12 02:19:29 8 Comments

I have quite a few controls scattered throughout many table cells in my table, and I was wondering if there's an easier way to dismiss the keyboard without having to loop through all my controls and resigning them all as the first responder. I guess the question is.. How would I get the current first responder to the keyboard?


@kirby 2010-09-14 01:13:30


[self.view endEditing:YES];

@JosephH 2010-09-14 04:28:01

This does seem to work (tested on ios4.1). Though it is only available on iOS 3.2 upwards. I'm not sure if this is actually guaranteed to work, or is just a non-guaranteed side effect from calling an API in a way that it's not documented to work.

@antsyawn 2010-12-31 01:18:47

This worked perfectly for me, and is certainly preferable to some of the other answers to this question. The docs say it's available in iOS 2.0 and later.

@joshis 2011-07-19 19:34:01

As the documentation says: "This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign." - so this is IMO the correct answer to the original question (use-case: I have a form with millions of fields, well - ten... or so, and I need to dismiss the keyboard).

@carbonr 2012-01-28 13:23:30

This method works best on a signup form etc where there are so many text fields and you want to resign keyboard on tapping background. Thanks for this line of code

@Seventoes 2012-10-22 00:55:55

Changed the accepted answer to this. The old one (Nicholas Riley's) was the correct way before iOS 3.2.

@chrisben 2013-01-05 16:29:48

Answer is not wrong but a little incorrect, actually it should be: [self.view endEditing:YES]; as BOOL values are YES/NO in Objective-C.

@Michael Superczynski 2013-01-27 02:35:44

chrisben: You can use YES/NO, TRUE/FALSE, 0/1.

@Shawn 2014-03-27 15:15:20

...and if you are doing window manipulation, say from a modal singleton, you might want to just do it on the window: [[UIApplication sharedApplication].keyWindow endEditing:YES];

@RayChen 2015-08-05 04:17:11

Please check answer here…

@Travelling Man 2013-01-05 01:32:58

You can send a nil targeted action to the application, it'll resign first responder at any time without having to worry about which view currently has first responder status.


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

Swift 3.0:

UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)

Nil targeted actions are common on Mac OS X for menu commands, and here's a use for them on iOS.

@Leo Natan 2013-06-30 14:03:21

Best solution of the entire page.

@Ben Sinclair 2014-01-15 23:40:51

Absolutely the best way to get rid of keyboard from anywhere in iOS app.

@Jenn Eve 2015-02-24 09:22:10

Still works for me in iOS8. In my application, I have a side bar menu, and there are times that the "center" view controller shows keyboard. And I want to dismiss the keyboard if the side bar menu will show. So I added this code in my side bar menu Class' viewWillAppear method.

@bmjohns 2016-03-22 17:48:33

Still works in iOS 9. Great way to globally resign focus of a textfield and dismiss keyboard.

@Travelling Man 2017-06-08 21:04:15

And to continue this particular commenting streak.... Still works in iOS 10. :-)

@prakash 2009-06-24 08:42:47

This is a solution to make the keyboard go away when hit return in any textfield, by adding code in one place (so don't have to add a handler for each textfield):

consider this scenario:

i have a viewcontroller with two textfields (username and password). and the viewcontroller implements UITextFieldDelegate protocol

i do this in viewDidLoad

- (void)viewDidLoad 
    [super viewDidLoad];

    username.delegate = self;
    password.delegate = self;

and the viewcontroller implements the optional method as

- (BOOL)textFieldShouldReturn:(UITextField *)textField
    [textField resignFirstResponder];
    return YES;

and irrespective of the textfield you are in, as soon as i hit return in the keyboard, it gets dismissed!

In your case, the same would work as long as you set all the textfield's delegate to self and implement textFieldShouldReturn

@Seventoes 2009-07-16 07:03:36

Well I already have the problem solved, but in my case I wanted to dismiss the keyboard on my own (in code), and I didn't know which textField had first responder status.

@Peter Johnson 2015-05-11 13:18:32

There are cases when you may want to dismiss the keyboard without hitting "return", for example when a sidebar menu is about to be displayed

@ToolmakerSteve 2017-03-14 05:38:09

Edited to clarify that this answer is only relevant in situations where hitting "return" is an acceptable solution. [In my case, I also wanted to dismiss keyboard when user taps outside the field being edited; this answer does not help with that.]

@KeithC 2017-02-20 19:18:54

In swift 3 you can do the following

UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)

@tryKuldeepTanwar 2016-10-05 06:07:08


I found another simple way

simply declare a property :-

@property( strong , nonatomic) UITextfield *currentTextfield;

and a Tap Gesture Gecognizer:-

@property (strong , nonatomic) UITapGestureRecognizer *resignTextField;

In ViewDidLoad

_currentTextfield=[[UITextField alloc]init];
_resignTextField=[[UITapGestureRecognizer alloc]initWithTarget:@selector(tapMethod:)];

[self.view addGestureRecognizer:_resignTextField];

Implement the textfield delegate method didBeginEditing

 -(void)textFieldDidBeginEditing:(UITextField *)textField{



Implement Your Tap Gesture Method (_resignTextField)

 -(void)tapMethod:(UITapGestureRecognizer *)Gesture{

     [_currentTextfield resignFirstResponder];


@Manish Methani 2016-08-09 08:16:22

The best way to dismiss keyboard from UITableView and UIScrollView are:

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag

@Vaibhav Shiledar 2016-09-14 08:15:13

You have to use one of these methods,

[self.view endEditing:YES];


[self.textField resignFirstResponder];

@user3032314 2013-12-04 14:57:45

Even Simpler than Meagar's answer

overwrite touchesBegan:withEvent:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    [textField resignFirstResponder];`

This will dismiss the keyboardwhen you touch anywhere in the background.

@bharath gangupalli 2010-12-22 14:33:54

the easist way is to call the method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    if(![txtfld resignFirstResponder])
        [txtfld resignFirstResponder];

    [super touchesBegan:touches withEvent:event];

@Seventoes 2010-12-27 11:12:43

You don't need the 'if' there, resignFirstResponder will do it's thing anyways. Maybe you meant -(BOOL)canResignFirstResponder?

@YannSteph 2016-07-05 14:06:32

In swift :


@Shanmugaraja G 2016-07-01 10:02:30

Yes, endEditing is the best option. And From iOW 7.0, UIScrollView has a cool feature to dismiss the keyboard on interacting with the scroll view. For achieving this, you can set keyboardDismissMode property of UIScrollView.

Set the keyboard dismiss mode as:

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag

It has few other types. Have a look at this apple document.

@user4993619 2015-12-02 05:32:11

To dismiss a keyboard after the keyboard has popped up, there are 2 cases,

  1. when the UITextField is inside a UIScrollView

  2. when the UITextField is outside a UIScrollView

2.when the UITextField is outside a UIScrollView override the method in your UIViewController subclass

you must also add delegate for all UITextView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
  1. In a scroll view, Tapping outside will not fire any event, so in that case use a Tap Gesture Recognizer, Drag and drop a UITapGesture for the scroll view and create an IBAction for it.

to create a IBAction, press ctrl+ click the UITapGesture and drag it to the .h file of viewcontroller.

Here I have named tappedEvent as my action name

- (IBAction)tappedEvent:(id)sender {
      [self.view endEditing:YES];  }

the abouve given Information was derived from the following link, please refer for more information or contact me if you dont understand the abouve data.

@malex 2015-09-17 20:19:26

You should send endEditing: to working window being the subclass of UIView

[[UIApplication sharedApplication].windows.firstObject endEditing:NO];

@Matrosov Alexander 2016-03-16 15:42:01

I bet you meant YES instead )

@Eike 2015-03-31 08:16:43

And in swift we can do

UIApplication.sharedApplication().sendAction("resignFirstResponder", to: nil, from: nil, forEvent: nil)

@Shayan C 2016-10-18 19:24:45

I do not understand how the first responder works.. where do I put this? in my view controller or delegate?

@Eike 2016-10-19 16:55:30

Doesn't matter. You can put it where ever you want to. i.e. button action

@Murat KAYA 2015-03-24 11:23:47

Add A Tap Gesture Recognizer to your view.And define it ibaction

your .m file will be like

    - (IBAction)hideKeyboardGesture:(id)sender {
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in windows) [window endEditing:true];
    [[UIApplication sharedApplication].keyWindow endEditing:true];

It's worked for me

@mattsven 2015-03-09 06:04:36

A slightly more robust method I needed to use recently:

- (void) dismissKeyboard {
    NSArray *windows = [UIApplication sharedApplication].windows;

    for(UIWindow *window in windows) [window endEditing:true];

    //  Or if you're only working with one UIWindow:

    [[UIApplication sharedApplication].keyWindow endEditing:true];

I found some of the other "global" methods didn't work (for example, UIWebView & WKWebView refused to resign).

@Takide 2015-01-07 19:55:26

Here's what I use in my code. It works like a charm!

In yourviewcontroller.h add:

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

Now in the .m file, add this to your ViewDidLoad function:

- (void)viewDidLoad {
    //Keyboard stuff
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapRecognizer];

Also, add this function in the .m file:

- (void)handleSingleTap:(UITapGestureRecognizer *) sender
    [self.view endEditing:YES];

@Matthijs 2019-02-05 08:36:30

Thanks! But you should rename the function to 'didTapAnywhere' or the gesture recognizer to 'handleSingleTap' ;-)

@mmmanishs 2013-10-11 14:53:06

Subclass your textfields... and also textviews

In the subclass put this code..


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissKeyBoard) name:KEYBOARD_DISMISS object:nil];


    [[NSNotificationCenter defaultCenter] removeObserver:self name:KEYBOARD_DISMISS object:nil];

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self resignFirstResponder];

In the textfield delegates (similarly for textview delegates)

-(void)textFieldDidBeginEditing:(JCPTextField *)textField{
     [textField conformsToKeyboardDismissNotification];

- (void)textFieldDidEndEditing:(JCPTextField *)textField{
    [textField deConformsToKeyboardDismissNotification];

All set.. Now just post the notification from anywhere in your code. It will resign any keyboard.

@Jeremy 2010-09-14 17:07:57

@Nicholas Riley & @Kendall Helmstetter Geln & @cannyboy:

Absolutely brilliant!

Thank you.

Considering your advice and the advice of others in this thread, this is what I've done:

What it looks like when used:

[[self appDelegate] dismissKeyboard]; (note: I added appDelegate as an addition to NSObject so I can use anywhere on anything)

What it looks like under the hood:

- (void)dismissKeyboard 
    UITextField *tempTextField = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
    tempTextField.enabled = NO;
    [myRootViewController.view addSubview:tempTextField];
    [tempTextField becomeFirstResponder];
    [tempTextField resignFirstResponder];
    [tempTextField removeFromSuperview];


Amendment to my answer to included tempTextField.enabled = NO;. Disabling the text field will prevent UIKeyboardWillShowNotification and UIKeyboardWillHideNotification keyboard notifications from being sent should you rely on these notifications throughout your app.

@demon 2013-07-25 03:16:11

i donn't know if it works, but it seems a smart and easy way.

@Craig Miller 2012-07-16 12:55:15

You may also need to override UIViewController disablesAutomaticKeyboardDismissal to get this to work in some cases. This may have to be done on the UINavigationController if you have one.

@Nicholas Riley 2009-04-12 05:39:09

You can force the currently-editing view to resign its first responder status with [view endEditing:YES]. This hides the keyboard.

Unlike -[UIResponder resignFirstResponder], -[UIView endEditing:] will search through subviews to find the current first responder. So you can send it to your top-level view (e.g. self.view in a UIViewController) and it will do the right thing.

(This answer previously included a couple of other solutions, which also worked but were more complicated than is necessary. I've removed them to avoid confusion.)

@Kendall Helmstetter Gelner 2009-04-13 06:29:00

But that will only block the component from becoming firstResponder, not remove current firstResponder status from an element.

@Nicholas Riley 2009-04-13 15:29:38

What I meant in overriding becomeFirstResponder to store the first responder somewhere, so it can be accessed (in this case, resigned) later. The basic problem is that Cocoa Touch does not provide a way to ask a window or view what its first responder is.

@Jason George 2012-03-15 21:01:01

+1 Adding becomeFirstResponder resolved issues I was having with taps on other UITextFields.

@ftvs 2012-06-29 04:41:58

This is obsolete. The [self.view endEditing:YES]; answer is better.

@Nicholas Riley 2012-06-29 13:46:40

Agreed — can you tell I'm not actually an iOS developer? I fixed it since this answer seems to be getting used.

@Matej 2012-08-31 22:49:42

I have problem with [self.view endEditing:YES];. I trigger this inside UITableView when user starts scrolling. When that happens the scroll bar is small and I can scroll 'under' the table view. Releasing finger and scrolling again solves that but it is rather annoying..

@Ben Sinclair 2014-07-07 19:09:35

endEditing does not work either. At least did not work with UITableView

@Jan Cássio 2015-02-16 01:47:41

This is not obsolete, this answer explain exactly how [UIView endediting:] works. In one of apps I've developed, a search box was added in UINavigationViewController, if you just call [self.view endEditing:YES], as self your view controller, this will not work, instead, I called this method directly in view scope where search box was added, example: [self.mySearchBoxView endEditing:YES].

@ToolmakerSteve 2017-03-14 05:26:35

@JanCássio - FYI, you probably missed that this is not obsolete because in 2012 it was edited in response to the complaint that it was obsolete, to incorporate the suggested simpler/more-up-to-date answer.

@user1270998 2012-04-21 23:08:10

In your view controller's header file add <UITextFieldDelegate> to the definition of your controller's interface so that it conform to the UITextField delegate protocol...

@interface someViewController : UIViewController <UITextFieldDelegate>

... In the controller's implementation file (.m) add the following method, or the code inside it if you already have a viewDidLoad method ...

- (void)viewDidLoad
    // Do any additional setup after loading the view, typically from a nib.
    self.yourTextBox.delegate = self;

... Then, link yourTextBox to your actual text field

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
    if (theTextField == yourTextBox) {
        [theTextField resignFirstResponder];
    return YES;

@Ian Wilkinson 2012-03-29 21:28:31

A lot of overly-complicated answers here, perhaps because this is not easy to find in the iOS documentation. JosephH had it right above:

[[view window] endEditing:YES];

@Armen 2011-09-17 15:44:04

Quick tip on how to dismiss the keyboard in iOS when a user touches anywhere on the screen outside of the UITextField or keyboard. Considering how much real estate the iOS keyboard can take up, it makes sense to have an easy and intuitive way for your users to dismiss the keyboard.

Here's a link

@Jiulong Zhao 2019-08-16 17:25:00

Great! adding to AppDelegate make it works for ALL related views.

@Brett Levine 2011-06-27 22:00:56

To be honest, I'm not crazy about any of the solutions proposed here. I did find a nice way to use a TapGestureRecognizer that I think gets to the heart of your problem: When you click on anything besides the keyboard, dismiss the keyboard.

  1. In viewDidLoad, register to receive keyboard notifications and create a UITapGestureRecognizer:

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyboardWillShow:) name:
    UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:) name:
    UIKeyboardWillHideNotification object:nil];
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
  2. Add the keyboard show/hide responders. There you add and remove the TapGestureRecognizer to the UIView that should dismiss the keyboard when tapped. Note: You do not have to add it to all of the sub-views or controls.

    -(void) keyboardWillShow:(NSNotification *) note {
        [self.view addGestureRecognizer:tapRecognizer];
    -(void) keyboardWillHide:(NSNotification *) note
        [self.view removeGestureRecognizer:tapRecognizer];
  3. The TapGestureRecognizer will call your function when it gets a tap and you can dismiss the keyboard like this:

    -(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {    
        [textField resignFirstResponder];

The nice thing about this solution is that it only filters for Taps, not swipes. So if you have scrolling content above the keyboard, swipes will still scroll and leave the keyboard displayed. By removing the gesture recognizer after the keyboard is gone, future taps on your view get handled normally.

@Professor Todd 2011-07-15 03:18:32

To my @implementation I added @property (assign) UITapGestureRecognizer *tapRecognizer; and modified self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];

@TahoeWolverine 2011-08-26 14:24:34

I'm trying to do a similar thing, and I'm not getting the callback on tap. Curious: why is your property 'assign'?

@Shaunti Fondrisi 2015-08-11 17:48:54

Plus one, and furthermore this should be the default iOS behavior. Or at least add a simple way to toggle a dismiss button.

@lorean 2011-06-24 17:27:57

Tuck this away in some utility class.

+ (void)dismissKeyboard {
    [self globalResignFirstResponder];

+ (void) globalResignFirstResponder {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    for (UIView * view in [window subviews]){
        [self globalResignFirstResponderRec:view];

+ (void) globalResignFirstResponderRec:(UIView*) view {
    if ([view respondsToSelector:@selector(resignFirstResponder)]){
        [view resignFirstResponder];
    for (UIView * subview in [view subviews]){
        [self globalResignFirstResponderRec:subview];

@aslisabanci 2011-07-19 09:40:26

this is what I was looking for!

@n13 2015-09-29 05:15:38

Perfect, except you don't need the globalResignFirstResponderRec method at all, just call [view endEditing:YES] instead. It does the same thing.

@douyw 2016-05-17 08:19:56

Yeah! The final solution! I tried many ways, only this one works for me! Thank you very much!

@JJ Rohrer 2010-11-23 18:45:50

Jeremy's answer wasn't quite working for me, I think because I had a navigation stack in a tab view with a modal dialog on top of it. I'm using the following right now and it is working for me, but your mileage may vary.

 // dismiss keyboard (mostly macro)
[[UIApplication sharedApplication].delegate dismissKeyboard]; // call this in your to app dismiss the keybaord

// --- dismiss keyboard (in indexAppDelegate.h) (mostly macro)
- (void)dismissKeyboard;

// --- dismiss keyboard (in indexAppDelegate.m) (mostly macro)
// do this from anywhere to dismiss the keybard
- (void)dismissKeyboard {    // from:

    UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectZero];

    UIViewController *myRootViewController = <#viewController#>; // for simple apps (INPUT: viewController is whatever your root controller is called.  Probably is a way to determine this progragrammatically)
    UIViewController *uivc;
    if (myRootViewController.navigationController != nil) { // for when there is a nav stack
        uivc = myRootViewController.navigationController;
    } else {
        uivc = myRootViewController;

    if (uivc.modalViewController != nil) { // for when there is something modal
        uivc = uivc.modalViewController;

    [uivc.view  addSubview:tempTextField];

    [tempTextField becomeFirstResponder];
    [tempTextField resignFirstResponder];
    [tempTextField removeFromSuperview];
    [tempTextField release];


@JJ Rohrer 2011-10-12 03:21:13

Hmm - why the down-vote? I addressed specific real-world shortcomings in another answer with tested code. I can understand a lack of up-votes, but voting me into negative territory is real discouragement. I still hope the above answer helps someone else.

@M. Ryan 2010-09-10 21:04:44

You can recursively iterate through subviews, store an array of all UITextFields, and then loop through them and resign them all.

Not really a great solution, especially if you have a lot of subviews, but for simple apps it should do the trick.

I solved this in a much more complicated, but much more performant way, but using a singleton/manager for the animation engine of my app, and any time a text field became the responder, I would assign assign it to a static which would get swept up (resigned) based on certain other events... its almost impossible for me to explain in a paragraph.

Be creative, it only took me 10 minutes to think through this for my app after I found this question.

@Seventoes 2010-09-13 07:44:29

Solved this months ago ;) I ended up just saving the active field on focus and using that.

@cannyboy 2010-05-24 18:34:26

It's not pretty, but the way I resign the firstResponder when I don't know what that the responder is:

Create an UITextField, either in IB or programmatically. Make it Hidden. Link it up to your code if you made it in IB. Then, when you want to dismiss the keyboard, you switch the responder to the invisible text field, and immediately resign it:

  [self.invisibleField becomeFirstResponder];
  [self.invisibleField resignFirstResponder];

@CIFilter 2009-11-30 15:49:47

I hate that there's no "global" way to programmatically dismiss the keyboard without using private API calls. Frequently, I have the need to dismiss the keyboard programmatically without knowing what object is the first responder. I've resorted to inspecting the self using the Objective-C runtime API, enumerating through all of its properties, pulling out those which are of type UITextField, and sending them the resignFirstResponder message.

It shouldn't be this hard to do this...

Related Questions

Sponsored Content

95 Answered Questions

36 Answered Questions

[SOLVED] iOS - Dismiss keyboard when touching outside of UITextField

4 Answered Questions

8 Answered Questions

[SOLVED] Dismiss keyboard on touch anywhere outside UITextField

11 Answered Questions

1 Answered Questions

UITextField iOS: how to deal with the dismiss keyboard button?

1 Answered Questions

[SOLVED] How dismiss keyboard without losing focus. or at least show cursor

  • 2016-06-09 12:49:04
  • vsl
  • 330 View
  • 1 Score
  • 1 Answer
  • Tags:   ios objective-c

4 Answered Questions

3 Answered Questions

Sponsored Content