I have filled my view with ScrollView (same size as the view) and I'm stuck at how to resign first responder when user tap elsewhere in the View (or the scrollview). Any idea on how to do that ? I'm using the following method but it's not working as expected:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Thx for helping,
Stephane
When possible the best option is to make your UIView to inherit from UIButton instead.
You just need to change it in Interface Builder and immediately you will get the options to create an Action "Touch Up Inside" like any other button.
Your view will work as usual because UIButton is still a view but you will get notified when someone taps on the view. There you can call
To resign any keyboard.
Please note that if your view is an UIScrollView this solution is not for you.
This is the easiest way iv found that works consistently. Just add a tap gesture recognizer on the view and you're good to go.
}
For Swift 3 and Swift 4 you can do this:
Just create an
IBOutlet
or pointer to the text field and call[textField resignFirstResponder];
from where ever you want.This is what I do...