Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIKeyboard does not hide when I touch outside the keyboard

I'm not figuering out a way to hide the keyboard when I touch outside the keyboard, is there any event that could hide the keyboard if I click outside the keyboard. Thanks

like image 854
user784625 Avatar asked Jan 31 '12 09:01

user784625


People also ask

How do I hide the keyboard in Swift 4?

If you have a UITextField and you want to hide the keyboard when the user is pressing the return key, use the textFieldShouldReturn function. Be sure you have set the textField. delegate = self in your viewDidLoad() function.


2 Answers

In the ViewController.m write the touches began method.In that method write the resignFirstResponder

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

}
like image 103
Tendulkar Avatar answered Sep 27 '22 17:09

Tendulkar


Here is a better solution that is gonna work for any UITextField in your view controller. You can even add it to your base view controller (if you have got one) and it will work like a charm.

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

    UITouch *touch = [[event allTouches] anyObject];

    if (![[touch view] isKindOfClass:[UITextField class]]) {
        [self.view endEditing:YES];
    }
    [super touchesBegan:touches withEvent:event];
}
like image 23
Yas Tabasam Avatar answered Sep 27 '22 19:09

Yas Tabasam