How can I dismiss the keyboard if a user taps off the on-screen keyboard?

43,707

Solution 1

You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.

The code:

In viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                      action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard {
       [aTextField resignFirstResponder];
}

(Where aTextField is the textfield that is responsible for the keyboard)

OPTION 2

If you can't afford to add a gestureRecognizer then you can try this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [aTextField resignFirstResponder];
    }
}

Solution 2

The simplest solution I have used is this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

      [self.view endEditing:YES];

}

The endEditing command can be used on any view that contains your textfield as a subview. The other advantage of this method is that you don't need to know which textfield triggered the keyboard. So even if you have a multiple textfields, just add this line to the superview.

Based on the Apple documentation, I think this method exists specifically to solve this problem.

Solution 3

Add a tapGesture Recognizer but make sure cancelsTouchesInView = NO

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

Solution 4

You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.

The code:

In viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                      action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard {
       [self.view endEditing:true];
}

Solution 5

This is a Swift 4 solution:

 let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))

 self.view.addGestureRecognizer(tap)

And the dismissKeyboard

@objc func dismissKeyboard() {
    self.view.endEditing(true)
}
Share:
43,707
Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on July 09, 2022

Comments

  • Sheehan Alam
    Sheehan Alam almost 2 years

    I want to be able to dismiss the iPhone keyboard when the user taps anywhere outside of the keyboard. How can I go about doing this? I know I need to dismiss the responder, but need to know how to implement it when a user taps out of the keyboard space.

  • Sheehan Alam
    Sheehan Alam about 13 years
    very elegant solution w/ the gesture recognizer!
  • kmehta
    kmehta about 13 years
    This is great, however, the gesture action is eating all my button click events on the view. Any ideas?
  • IOS Rocks
    IOS Rocks about 11 years
    Good solution for dismissing the keyboard in ios.
  • Jim True
    Jim True about 11 years
    I thought this was great but it overrides any other inputs like CollectionViews too. See Matt Rees' comment below for fix.
  • Miros
    Miros about 10 years
    Extremely good point about the tapGesture.cancelsTouchesInView = NO; Thanks a bunch!
  • testing
    testing over 9 years
    What about a UIScrollView?
  • StackUnderflow
    StackUnderflow about 7 years
    This is the best solution no DOUBT!
  • Vadim Bulavin
    Vadim Bulavin over 6 years
    Instead of [touches anyObject] use touches.first in Swift.
  • Reza Dehnavi
    Reza Dehnavi over 5 years
    Try this for UIScrollView scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;