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
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.
In the ViewController.m write the touches began method.In that method write the resignFirstResponder
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[textField resignFirstResponder];
}
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With