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 开发者_StackOverflow中文版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
I found the answer here:
If your ultimate aim is just to resign the first responder, this should work:
[self.view endEditing:YES]
The endEditing(_:)
method is designed right for it
Causes the view (or one of its embedded text fields) to resign the first responder status.
For a more robust and clean solution add a tap gesture recognizer to your primary view.
This will work better with nested views and will be cleaner than secret buttons in code and UI builder.
In your view did load:
UITapGestureRecognizer* tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[tapBackground setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapBackground];
..and define your target action to be triggered on tap:
-(void) dismissKeyboard:(id)sender
{
[self.view endEditing:YES];
}
The best option is the shortest way ;)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
Using Swift you can do this:
func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboardByTappingOutside))
self.view.addGestureRecognizer(tap)
}
@objc func hideKeyboardByTappingOutside() {
self.view.endEditing(true)
}
UIViewController
inherits from UIResponder
so a naive way (I am pretty sure is not the smartest way to do it) would be by overwriting the following methods (at least 1 of them)
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
- touchesCancelled:withEvent:
Next you could get the touched view by doing
UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];
finally resign the first responder if that view is not your text field
if(touchedView != textField){
[textField resignFirstResponder];
}
_
Demerits of this approach:
You will have to handle the "tap" by yourself. (Same problem as the old iOS 3.1 or earlier). You will have to come up with your own implementation to differentiate single taps from drags, swipes, double taps, long taps, etc. Is not hard to get it working well but it is not likely you get it exactly the same way Apple detects taps (timings, distances, thresholds count!) However, that depends on your needs.
If your view structure is simple enough then you could add a gesture recognizer to the container view and resign the first responder every time the handler is called :)
Hope this helps
Nobody has yet presented the best answer here:
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
From the documentation on this method:
Normally, this method is invoked by a UIControl object that the user has touched. The default implementation dispatches the action method to the given target object or, if no target is specified, to the first responder. Subclasses may override this method to perform special dispatching of action messages.
I bolded the relevant part of that paragraph. By specifying nil
for the to:
parameter, we automatically call the given selector on the first responder, wherever it is, and we don't need to have any knowledge of where in the view hierarchy it is. This can also be used to invoke other methods on the first responder as well, not just cause it to resign first responder status.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.TextField resignFirstResponder];
return YES;
}
Give a unique Tag to your UITextfield (i.e. 333), then to resign the first responder do this:
UITextField *textField = (UITextField *)[self.view viewWithTag:333];
[textField resignFirstResponder];
Just create an IBOutlet
or pointer to the text field and call [textField resignFirstResponder];
from where ever you want.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapTodissmiss)];
[self.view addGestureRecognizer:tap];
and selector
-(void)TapTodissmiss{
[self.txtffld resignFirstResponder];
}
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.
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.
Create your IBOutlet in ViewController.h file like below:
//YOUR ViewController.h FILE
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//WIRE THIS WITH YOUR UITextField OBJECT AT YOUR .xib FILE
IBOutlet UITextField *yourIBOutletUITextField;
}
@end
Add this to your ViewController.m file:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[yourIBOutletUITextField resignFirstResponder];
}
like below:
//YOUR ViewController.m FILE
#import "ViewController.h"
@implementation ViewController
//ADD THIS METHOD BELOW
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[yourIBOutletUITextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
For my implementation, [self.view endEditing:YES] did not work.
To finally make sure the keyboard was hidden, I had to make one of my views the first responder, and then immediately make it resign as per the function below:
-(void) hideKeyboardPlease{
[uiTextTo becomeFirstResponder];
[uiTextTo 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];
}
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.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
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];
}
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
精彩评论