71

normally when you touch the text input area, the keyboard pops up and when you touch the empty area of screen, the keyboard disappeared. How to make that happen?

just like what we experienced on iphone safari...

Thank you

swiftBoy
  • 35,607
  • 26
  • 136
  • 135

11 Answers11

114

Updated way (recommended):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   [self.view endEditing:YES];
}

This will end editing on all subviews and resign the first responder.

Other way (enumerating over all text views):

Here's a step by step for it:

Add an IBAction to your view controller, such as - (IBAction)backgroundTouch:(id)sender

In the backgroundTouch action, you need to send the resignFirstResponder message to all of the text boxes in your view. This is unfortunate but necessary since there's currently no way to retrieve the object that currently has FirstResponder status. It should look something like this:

- (IBAction)backgroundTouch:(id)sender {
  [someTextBox resignFirstResponder];
  [anotherTextBox resignFirstResponder];
}

Add a button control to the view, size it to cover the entire visible area (except for the status bar or any tab or navigation controllers). Select the button and then go to the Layout Menu and select Send To Back. Also set the button's Type to custom, which is invisible if you don't specifically supply any drawing code for it.

Connect the Button's Touch Up Inside event to the backgroundTouch: action and try it out.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
HitScan
  • 8,651
  • 3
  • 22
  • 17
  • 51
    Short cut found on another post: [self.view endEditing:YES]; will end editing in all subviews and resign first responder. Its a lot more clear than enumerating all of your text boxes and won't break when you add fields later! – Peter DeWeese Sep 02 '10 at 16:37
  • Thanks to HitScan for elaboration and Many Thanks to Peter to share the shortcut with us!! – necixy Mar 29 '11 at 07:02
  • On the first solution, `touchesBegan:withEvent:` shouldn't be calling `[super touchesBegan:touches withEvent:event]`? – Gonzo Jul 27 '15 at 16:42
73

Use this simple Solution

For Swift 4 :

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
   }

For Swift 2 :

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

For Objective C :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   [self.view endEditing:YES];
}  

Works with UITextField, UITextView and all subviews.

Shahid Aslam
  • 2,585
  • 3
  • 24
  • 31
  • 1
    Simple & elegant solution.. Works like charm! – Mahendra Liya Jul 16 '12 at 12:46
  • How do I use this? I have a ViewController but it's never called. So do I have to subclass UIView and override this method, and then in the IB make my view of this subclass? – Sandy May 13 '13 at 20:08
  • @Rob just add your code in you viewcontroller.m no linking with view required. It should work – Shahid Aslam May 22 '13 at 14:26
  • 1
    All around the web, this is the only bug free solution (most others cause conflict with buttons not calling their selectors) and most elegant between them all, I salute you – SAFAD Mar 11 '14 at 20:24
16

Forget about putting a button in the background there simple solution

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.textfieldName resignFirstResponder];
}
joaquin
  • 82,968
  • 29
  • 138
  • 152
alex
  • 169
  • 1
  • 2
4

there's a tutorial about that problem (also concerning the numerical keyboard with no DONE key) over here

4

For > iOS 4.0 there is another solution (and maybe better) With this code, single tap anywhere in view to hide keyboard.

// single tap to resign keyboard
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithBlock:^(UIGestureRecognizer *rec){
            [input_text resignFirstResponder];
        }];
self.singleTapRecognizer.numberOfTapsRequired = 1;
self.singleTapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:singleTapRecognizer];
Val
  • 21,938
  • 10
  • 68
  • 86
2

Send resignFirstResponder to the control.

Chris Lundie
  • 6,023
  • 2
  • 27
  • 28
1

You can do this way also

Hide keyBoard by touching background in view

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self view] endEditing:YES];
}
Antonio
  • 71,651
  • 11
  • 148
  • 165
Vijay Dokrimare
  • 287
  • 3
  • 6
1

The Following code works fine with xamarin iOS project :

public override void TouchesBegan (NSSet touches, UIEvent evt){
    View.EndEditing (true);
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
T. Naik
  • 31
  • 1
  • 2
0

I could not compile Val's answer above using the 'initWithBlock:' selector.

This code works for me. Add this to the method defining the view - note that my view is composed and the subview that I want to respond to the single tap is 'chatView'. The input field view that uses the keyboard is named 'chatInput'.

    // single tap to resign (hide) the keyboard
    UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    singleTapRecognizer.numberOfTouchesRequired = 1;
    singleTapRecognizer.cancelsTouchesInView = NO;
    [chatContent addGestureRecognizer:singleTapRecognizer];

Then add the tap-handling method.

/**
 * Handles a recognized single tap gesture.
 */
- (void) handleTapFrom: (UITapGestureRecognizer *) recognizer {
    // hide the keyboard
    NSLog(@"hiding the keyboard");
    [chatInput resignFirstResponder]; 
}
0

This solution should work fine if you are working with swift and iOS 8/9:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
         self.view.endEditing(true)
    }
gbdavid
  • 1,639
  • 18
  • 40
0

I was having this problem too, it was very annoying. But I figured out how to get rid of the keyboard on accident. Say you are texting John, and there's a keyboard in the way from you viewing your conversations and therefore reducing the screen you can see. Well then click on messages in the uppwer left, then select a different person you have a record of texting with, then hit messages again in the upper left. Then go back and select John. No you're back at the conversation with John, by the keyboard is gone. Seams like a lot but it's pretty quick. You'd think they'd just put a hide button to on the keyboard lol Hope that helps, or was clear enough.