Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide the keyboard when empty area is touched on iphone

People also ask

How do I hide my keyboard on Iphone?

To hide it, slide your finger down from above the text-entry box and the keyboard will start to disappear. Carry on until only the text-entry box is left. To make the keyboard reappear, tap the text-entry box and it will shoot right back up so text can be entered again.

How do you dismiss a keyboard?

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.


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.


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.


Forget about putting a button in the background there simple solution

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.textfieldName resignFirstResponder];
}

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


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];

Send resignFirstResponder to the control.