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 id
Just create an IBOutlet
or pointer to the text field and call [textField resignFirstResponder];
from where ever you want.
I am posting this as a separate answer because a lot of the other answers recommend using [textField resignFirstResponder]
. This is causing an invalid capability error in Xcode 11.5 with the incomprehensible addition "Unable to insert COPY_SEND"
On a iPad with iOS 9.3.6:
2020-05-23 20:35:01.576 _BSMachError: (os/kern) invalid capability (20)
2020-05-23 20:35:01.580 _BSMachError: (os/kern) invalid name (15)
On a iPad with iPadOS 13.5:
2020-05-23 20:38:49 [Common] _BSMachError: port 12f0f; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
On a iPhone with iOS 13.5:
2020-05-23 20:43:34 [Common] _BSMachError: port d503; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
[textField resignFirstResponder]
cannot be used anymore.
Create a big button behind it all, and the button's callback calls resignFirstResponder
to every input you have. Just make sure to put everything that the user interacts with on top of the button.
Of course the button will be custom rect and transparent.
I would look into implementing an IBOutletCollection called resignables and set every input to that collection. And the button callback iterates over that collection calling resignFirstResponder
to them all.
Edit: if it is a re-sizable scroll view, remember to correctly set the re-size markers in the button's view option, that way the button will always expand and contract to the scrollview's height and width
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
[self.view endEditing:YES];
To resign any keyboard.
Please note that if your view is an UIScrollView this solution is not for you.
Update
I found another simple way
simply declare a property :-
@property( strong , nonatomic) UITextfield *currentTextfield;
and a Tap Gesture Gecognizer:-
@property (strong , nonatomic) UITapGestureRecognizer *resignTextField;
In ViewDidLoad
_currentTextfield=[[UITextField alloc]init];
_resignTextField=[[UITapGestureRecognizer alloc]initWithTarget:@selector(tapMethod:)];
[self.view addGestureRecognizer:_resignTextField];
Implement the textfield delegate method didBeginEditing
-(void)textFieldDidBeginEditing:(UITextField *)textField{
_currentTextfield=textField;
}
Implement Your Tap Gesture Method (_resignTextField)
-(void)tapMethod:(UITapGestureRecognizer *)Gesture{
[_currentTextfield resignFirstResponder];
}
This is what I do...
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllKeyboards)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
-(void) hideAllKeyboards {
[textField_1 resignFirstResponder];
[textField_2 resignFirstResponder];
[textField_3 resignFirstResponder];
.
.
.
[textField_N resignFirstResponder];
}