I have a UITableView with UITextFields as cells. I would like to dismiss the keyboard when the background of the UITableView is touched. I'm trying to do this by creating a UIButton the size of the UITableView and placing it behind the UITableView. The only problem is the UIButton is catching all the touches even when the touch is on the UITableView. What am I doing wrong?
Thanks!
This is easily done by creating a UITapGestureRecognizer object (by default this will detect a "gesture" on a single tap so no further customization is required), specifying a target/action for when the gesture is fired, and then attaching the gesture recognizer object to your table view.
E.g. Perhaps in your viewDidLoad method:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
And the hideKeyboard method might look like this:
- (void) hideKeyboard {
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
...
...
}
Note that the gesture is not fired when touching inside a UITextField object. It is fired though on the UITableView background, footer view, header view and on UILabels inside cells etc.
The UITapGestureRecognizer solution works with table cell selection if you set:
gestureRecognizer.cancelsTouchesInView = NO;
Here is a best way to do this.
Just do this
[self.view endEditing:YES];
or
[[self.tableView superView] endEditing:YES];
You can also do it from Storyboard:
As UITableView is a subclass of UIScrollView, implementing one delegate method below provides an extremely easy, quick solution. No need to even involve resignFirstResponder since view hierarchy introspects and finds the current responder and asks it to resign it's responder status.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
And remember to add UIScrollViewDelegate to header file.
tableView.keyboardDismissMode = .onDrag
Firstly, listen for scrollViewWillBeginDragging in your UIViewController by adding the UIScrollViewDelegate:
In .h file:
#interface MyViewController : UIViewController <UIScrollViewDelegate>
In .m file:
- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView {
[self dismissKeyboard];
}
Then listen for other interactions:
- (void)setupKeyboardDismissTaps {
UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
swipeUpGestureRecognizer.cancelsTouchesInView = NO;
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.tableView addGestureRecognizer:swipeUpGestureRecognizer];
UISwipeGestureRecognizer *swipeDownGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
swipeDownGestureRecognizer.cancelsTouchesInView = NO;
swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.tableView addGestureRecognizer:swipeDownGestureRecognizer];
UISwipeGestureRecognizer *swipeLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
swipeLeftGestureRecognizer.cancelsTouchesInView = NO;
swipeLeftGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:swipeLeftGestureRecognizer];
UISwipeGestureRecognizer *swipeRightGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
swipeRightGestureRecognizer.cancelsTouchesInView = NO;
swipeRightGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.tableView addGestureRecognizer:swipeRightGestureRecognizer];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tapGestureRecognizer];
}
Then implement dismissKeyboard:
- (void)dismissKeyboard {
NSLog(#"dismissKeyboard");
[yourTextFieldPointer resignFirstResponder];
}
And if, like me, you wanted to dismiss the keyboard for a UITextField inside a custom table cell:
- (void)dismissKeyboard {
NSLog(#"dismissKeyboard");
CustomCellClass *customCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[customCell.textFieldInCell resignFirstResponder];
}
Hope that helps anyone searching!!
Here's the swift version for your coding pleasure:
It adds a tap gesture recognizer then dismisses the keyboard. No outlet for the TextField is required!
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
view.endEditing(true)
}
sender.cancelsTouchesInView = false
}
There is Swift 3 version without blocking taps on cells.
In viewDidLoad() method:
let dismissKeyboardGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
dismissKeyboardGesture.cancelsTouchesInView = false
tableView.addGestureRecognizer(dismissKeyboardGesture)
And hideKeyboard looks like this:
func hideKeyboard() {
view.endEditing(true)
}
I did it like this:
Create a method in your TableViewController to deactivate first responder (which would be your TextBox at that point)
- (BOOL)findAndResignFirstResonder:(UIView *)stView {
if (stView.isFirstResponder) {
[stView resignFirstResponder];
return YES;
}
for (UIView *subView in stView.subviews) {
if ([self findAndResignFirstResonder:subView]) {
return YES;
}
}
return NO;
}
In tableView:didSelectRowAtIndexPath: call the previous method:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
[self findAndResignFirstResonder: self.view];
...
}
I had a UITableViewController and implementing touchesBegan:withEvent: didn't work for me.
Here's what worked:
Swift:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
view.endEditing(true)
}
Objective-C:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.view endEditing:YES];
}
#interface DismissableUITableView : UITableView {
}
#end
#implementation DismissableUITableView
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.superview endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
#end
Then make sure that in your Nib file you set the type of your UITableView to DismissableUITableView .....maybe i could have thought of a better name for this class, but you get the point.
If you are targeting iOS7 you can use one of the following:
tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
The former will animate the keyboard off screen when the table view is scrolled and the later will hide the keyboard like the stock Messages app.
Note that these are from UIScrollView, which UITableView inherits from.
Try this:
viewDidLoad(){
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tableView.addGestureRecognizer(tap)
}
//Calls this function when the tap is recognized.
#objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
UITableView is a subclass of UIScrollView.
The way I did it was to listen for a scroll event by the user and then resignFirstResponder. Here's the UIScrollViewDelegate method to implement in your code;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
When approaching these sorts of problems I've found the best way is to research the delegate protocols for each object and those of the parent classes (in this case UITableViewDelegate, UIScrollViewDelegate. The number of events NS objects fires is quite large and comprehensive. It's also easier implementing a protocol then subclassing anything.
I had the same problem and here's my solution, it works perfectly for me:
In the view or view controller that you implemented <UITextFieldDelegate>
(In my case I have a custom UITableViewCell called TextFieldCell),
Declare the UITapGestureRecognizer as a property:
#interface TextFieldCell : UITableViewCell <UITextFieldDelegate>
{
UITextField *theTextField;
UITapGestureRecognizer *gestureRecognizer;
}
#property (nonatomic,retain) UITextField *theTextField;
#property (nonatomic,retain) UITapGestureRecognizer *gestureRecognizer;
And initialize it in your view/controller:
self.gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeKeyboard:)];
In the - (void)textFieldDidBeginEditing:(UITextField *)textField method, use superView to move up to your tableView and call addGestureRecognizer:
[self.superview.superview addGestureRecognizer:gestureRecognizer];
And in the - (void)textFieldDidEndEditing:(UITextField *)textField, just remove the gesture recognizer:
[self.superview.superview removeGestureRecognizer:gestureRecognizer];
Hope it helps.
I wanted my cell to open the keyboard when any part of the cell was selected and close it if you clicked anywhere off the cell. To open the keyboard:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected)
{
[self.textField becomeFirstResponder];
}
}
(NOTE: I've subclassed the cell but you can easily achieve this in the tableView:didSelectRowAtIndexPath: delegate method of UITableView)
Doing this meant that with the top solutions if you clicking on the cell twice the keyboard would shake as, first the gesture recogniser tried to close the keyboard, and second the cell was reselected and tried open the keyboard.
Solution is to check whether the click occurred inside the currently selected cell:
- (void)viewDidLoad
{
[super viewDidLoad];
//gesture recognizer to close the keyboard when user taps away
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard:)];
tap.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tap];
}
-(void)dismissKeyboard:(UIGestureRecognizer*)tapGestureRecognizer
{
if (!CGRectContainsPoint([self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]].frame, [tapGestureRecognizer locationInView:self.tableView]))
{
[self.view endEditing:YES];
}
}
I've found a solution that works great.
Is needed to use the UIGestureRecognizerDelegate and the method – gestureRecognizer:shouldReceiveTouch:.
Add the gesture recognizer to the TableView as follows:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
tapGestureRecognizer.cancelsTouchesInView = NO;
tapGestureRecognizer.delegate = self;
[self.suggestedTableView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
Then, implement the shouldReceiveTouch delegate method to reject touches that are performed in UITableViewCell class. The hideKeyboard method only will be called when the touch has been performed outside UITableViewCell class.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if([touch.view isKindOfClass:[UITableViewCell class]]) {
return NO;
}
// UITableViewCellContentView => UITableViewCell
if([touch.view.superview isKindOfClass:[UITableViewCell class]]) {
return NO;
}
// UITableViewCellContentView => UITableViewCellScrollView => UITableViewCell
if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) {
return NO;
}
return YES; // handle the touch
}
- (void) hideKeyboard{
[textField resignFirstResponder];
}
UITableView has a handy backgroundView property, with which I achieved this behavior without messing with cell selection, as shown below in Swift:
let tableBackTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
tableView.backgroundView = UIView()
tableView.backgroundView?.addGestureRecognizer(tableBackTapRecognizer)
I was searching for the solution and did not find anything that would fit my code, so I did it like this:
http://82517.tumblr.com/post/13189719252/dismiss-keyboard-on-uitableview-non-cell-tap
It's basically a combination of before-mentioned approaches but does not require to subclass anything or to create background buttons.
Simply using a UITapGestureRecognizer and cancelsTouchesInView = NO means that taps on cells and UITextViews also trigger the hide. This is bad if you have multiple UITextViews and you tap on the next one. The keyboard will start to hide and then the next textView becomes the firstResponder and the keyboard becomes visible again. To avoid this, check the tap location and only hide the keyboard if the tap isn't on a cell:
// init
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapTableView:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tapRecognizer];
// Hide on tap
- (void)didTapTableView:(UITapGestureRecognizer *)tap
{
CGPoint point = [tap locationInView:tap.view];
[self.view endEditing:!CGRectContainsPoint([self.tableView rectForRowAtIndexPath:[self.tableView indexPathForRowAtPoint:point]], point)];
}
In order for scrollViewWillBeginDragging: to be triggered, the tableView's scrollEnabled property must be YES
// Hide on scroll
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
Swift 4/4.2/5
You can also dismiss the keyboard when a cell is tapped - prior to doing anything else.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
view.endEditing(true)
// Do something here
}
tableView.keyboardDismissMode = .onDrag // .interactive
Why do you want to create a table full of textfields? You should be using a detailed view for each row that contains the text fields.
When you push your detailedview, ensure that you call "[myTextField becomeFirstResponder]" so that the user can start editing with just one click away from the table list.
If you're willing to subclass (ugh!) your table view, something like this might work:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
BOOL backgroundTouched = YES;
for (UITouch *touch in touches) {
CGPoint location = [touch locationInView:self];
for (UITableViewCell *cell in self.visibleCells) {
if (CGRectContainsPoint(cell.frame, location)) {
backgroundTouched = NO;
break;
}
}
}
if (backgroundTouched) {
for (UITableViewCell *cell in self.visibleCells) {
// This presumes the first subview is the text field you want to resign.
[[cell.contentView.subviews objectAtIndex:0] resignFirstResponder];
}
}
[super touchesBegan:touches withEvent:event];
}
If you want to dismiss the keyboard while return key is pressed,you can simply add the following code in textField should return method i.e.:
- (BOOL)textFieldShouldReturn:(UITextField *)atextField
{
[textField resignFirstresponder];
}
Some textfields might have a picker view or some other as a subview,so in that case the above method doesn't work so in that case we need to make use of UITapGestureRecognizer class i.e. add the following code snippet to viewDidLoad method i.e.:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Now simply add the resign responder to the selector method i.e.:
-(void)dismissKeyboard
{
[textField resignFirstResponder];
}
Hope it helps,thanks :)
Many interesting answers. I would like to compile different approaches into the solution that i thought best fit a UITableView scenario (it's the one I usually use):
What we usually want is basically to hide the keyboard on two scenarios: on tapping outside of the Text UI elements, or on scrolling down/up the UITableView. The first scenario we can easily add via a TapGestureRecognizer, and the second via the UIScrollViewDelegate scrollViewWillBeginDragging: method.
First order of business, the method to hide the keyboard:
/**
* Shortcut for resigning all responders and pull-back the keyboard
*/
-(void)hideKeyboard
{
//this convenience method on UITableView sends a nested message to all subviews, and they resign responders if they have hold of the keyboard
[self.tableView endEditing:YES];
}
This method resigns any textField UI of the subviews within the UITableView view hierarchy, so it's more practical than resigning every single element independently.
Next we take care of dismissing via an outside Tap gesture, with:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupKeyboardDismissGestures];
}
- (void)setupKeyboardDismissGestures
{
// Example for a swipe gesture recognizer. it was not set-up since we use scrollViewDelegate for dissmin-on-swiping, but it could be useful to keep in mind for views that do not inherit from UIScrollView
// UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
// swipeUpGestureRecognizer.cancelsTouchesInView = NO;
// swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
// [self.tableView addGestureRecognizer:swipeUpGestureRecognizer];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
//this prevents the gestureRecognizer to override other Taps, such as Cell Selection
tapGestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tapGestureRecognizer];
}
Setting tapGestureRecognizer.cancelsTouchesInView to NO is to avoid the gestureRecognizer from overriding the normal inner workings of the UITableView (for example, not to interfere with cell Selection).
Finally, to handle hiding the keyboard on Scrolling up/down the UITableView, we must implement the UIScrollViewDelegate protocol scrollViewWillBeginDragging: method, as:
.h file
#interface MyViewController : UIViewController <UIScrollViewDelegate>
.m file
#pragma mark - UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self hideKeyboard];
}
I hope it helps! =)
Here's how I finally made works. I combined suggestions and codes from different answers.
Features: dismissing keyboard, moving text fields above keyboard while editing and setting "Next" and "Done" keyboard return type.REPLACE "..." with more fields
static const CGFloat ANIMATION_DURATION = 0.4;
static const CGFloat LITTLE_SPACE = 5;
CGFloat animatedDistance;
CGSize keyboardSize;
#interface ViewController () <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *firstNameTXT;
.....// some other text fields
#property (weak, nonatomic) IBOutlet UITextField *emailTXT;
#end
#implementation ViewController
- (void)viewDidLoad{
.....
// add tap gesture to help in dismissing keyboard
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapScreen:)];// outside textfields
[self.view addGestureRecognizer:tapGesture];
// set text fields return key type to Next, last text field to Done
[self.firstNameTXT setReturnKeyType:UIReturnKeyNext];
.....
[self.emailTXT setReturnKeyType:UIReturnKeyDone];
// set text fields tags
[self.firstNameTXT setTag:0];
....// more text fields
[self.emailTXT setTag:5];
// add keyboard notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
// dismiss keyboard when tap outside text fields
- (IBAction)tapScreen:(UITapGestureRecognizer *)sender {
if([self.firstNameTXT isFirstResponder])[self.firstNameTXT resignFirstResponder];
...
if([self.emailTXT isFirstResponder])[self.emailTXT resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if(textField.returnKeyType==UIReturnKeyNext) {
// find the text field with next tag
UIView *next = [[textField superview] viewWithTag:textField.tag+1];
[next becomeFirstResponder];
} else if (textField.returnKeyType==UIReturnKeyDone || textField.returnKeyType==UIReturnKeyDefault) {
[textField resignFirstResponder];
}
return YES;
}
// Moving current text field above keyboard
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField{
CGRect viewFrame = self.view.frame;
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat textFieldBottomLine = textFieldRect.origin.y + textFieldRect.size.height + LITTLE_SPACE;//
CGFloat keyboardHeight = keyboardSize.height;
BOOL isTextFieldHidden = textFieldBottomLine > (viewRect.size.height - keyboardHeight)? TRUE :FALSE;
if (isTextFieldHidden) {
animatedDistance = textFieldBottomLine - (viewRect.size.height - keyboardHeight) ;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
return YES;
}
-(void) restoreViewFrameOrigionYToZero{
CGRect viewFrame = self.view.frame;
if (viewFrame.origin.y != 0) {
viewFrame.origin.y = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
-(void)keyboardDidShow:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
}
-(void)keyboardDidHide:(NSNotification*)aNotification{
[self restoreViewFrameOrigionYToZero];// keyboard is dismissed, restore frame view to its zero origin
}
#end
#mixca's answer is very useful but what if i've something different from UITextField. I think best way to handle it by searching all subviews of main view with recursive function, check example below
- (BOOL)findAndResignFirstResponder {
if (self.isFirstResponder) {
[self resignFirstResponder];
return YES;
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResponder]) {
return YES;
}
}
return NO;
}
and also you can put this method to your utility class and can use from tap gesture like #mixca's answer..
Related
I am using UITapGestureRecognizer for detecting which UIView was tapped on my screen but for some reason it only detects the parent view tap, for example below code logs only parent views tag. How do i detect subview taps which are present on main view. Please suggest.
Inside View did load :-
UITapGestureRecognizer *viewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionForViewTapped:)];
[self.view addGestureRecognizer:viewTapRecognizer];
Method outside view did load.
-(void) actionForViewTapped:(UITapGestureRecognizer*)sender {
NSLog(#"view tapped");
UIView *view = sender.view;
NSLog(#"view tag is %lu", view.tag); //Always prints parent view tag.
if(view.tag == 10){
NSLog(#"tag1 tapped"); //Not called
}
if(view.tag == 20){
NSLog(#"tag 2 tapped"); //Not called
}
}
We have more options to find detecting on sub view by tap gesture
CHOICE 1:Directly tap to SubView
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapSubView)];
tapGesture.numberOfTapsRequired = 1;
[subView addGestureRecognizer:tapGesture];
CHOICE 2:Finding tap on SubView through Parent View
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapSubView)];
tapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapGesture];
-(void)tapSubView:(UITapGestureRecognizer *)sender
{
UIView* view = sender.view;
CGPoint loc = [sender locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
//OR
CGPoint point = [sender locationInView:sender.view];
UIView *viewTouched = [sender.view hitTest:point withEvent:nil];
if ([viewTouched isKindOfClass:[self.view class]])
{
NSLog(#"the subView is called");
}
else
{
NSLog(#"the subView is not called");
}
}
Printed Output is
the subView is called
CHOICE 3:Find Tap Detection using Delegate methods of Gesture
First You have to add the GestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if([touch.view isKindOfClass: [self.view class]] == YES)
{
return YES; // return YES (the default) to allow the gesture recognizer to examine the touch object
}
else {
return NO; //NO to prevent the gesture recognizer from seeing this touch object.
}
}
The gesture recognizer is only associated with one specific view, which means it will only recognize touches on the view it is added to. If you want to know which subview was touched, then you will need to do a couple of things:
Set userInteractionEnabled = false for each subview. This will make it so that every touch on a subview is passed up to the parent view, and the touch will be recognized by the gesture recognizer.
There isn't enough information on your view hierarchy or layout to know exactly how to proceed from here, but you can use one or some of these methods to determine which view was touched: UIView.hitTest(_:with:), UIView.point(inside:with:), CGRectContainsPoint() or UIGestureRecognizer.location(in:). For example, if the subviews do not overlap each other, you could use the following code snippet to test if the touch was in a particular view:
let location = tapGesture.locationInView(parentView)
if CGRectContainsPoint(subview1, location) {
// subview1 was touched
}
I am new in iOS development. I want to hide the keyboard when tapping outside of a TextField. My TextField is in a cell from an UITableView.
I have tried to follow some of those links, however without any success--
Dismiss keyboard on touch anywhere outside UITextField
Dismiss keyboard by touching background of UITableView
Hide keyboard when scroll UITableView
I am trying to find the simplest way possible.
Thanks in advance
This is the simplest way to dismiss keyboard
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
It's not about touch, only working when scroll
TableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
also have
UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
Add delegate class UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.
you can use Tap gesture to hide keyboard.
- (void) tapGesture : (UIGestureRecognizer *) gestureRecognizer {
for (UIView *subview in view.subviews) {
if([subview isKindOfClass : [UITextField class]] ) {
UITextField *tf = (UITextField *) subview;
[tf resignFirstResponder];
}
}
}
Try This code
Write following code in viewDidLoad and add UIGestureRecognizerDelegate in .h file.
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[singleFingerTap setDelegate:self];
[self.view addGestureRecognizer:singleFingerTap];
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Delegates of keyboard Appearances and disappearances
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
tblview.tag = 1;
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
tblview.tag = 0;
}
UITapGestureRecognizer event function for hide keyboard
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
blview.tag = 0;
[self.view endEditing:YES];
}
UIGestureRecognizer delegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if(tblview.tag == 1){
return TRUE;
}
else{
return FALSE;
}
}
I am using the solution in two parts:
To dismiss keyboard on tableview/collectionview tap:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView= NO;
[self.collectionView addGestureRecognizer:gestureRecognizer];
(Don't forget cancelsTouchesInView set to NO to get touch event of tableview/collection view)
To dismiss keyboard on scroll (as tableview/collectionview are subclass of UIScrollView):
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self.view endEditing:YES];
}
Hope it helps somebody.
This will help you..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
The easiest way is to alloc a tap Gesture in viewDidLoad and then hide keyboard
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[_tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
Or on github you certainly found a library that hide your keyboard
I'm trying to add UITableView inside UIView but I got problem with gesture recognizer. This is my code:
_subView = [[UIView alloc] initWithFrame:CGRectMake(0, screenRect.size.height-100, screenRect.size.width, 300)];
_subView.backgroundColor = [UIColor colorWithRed:0.110f green:0.192f blue:0.298f alpha:1.00f];
[self.view addSubview:_subView];
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(subViewToggle)];
[_subView addGestureRecognizer:singleFingerTap];
_moreTable = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenRect.size.width, 240)];
//Transforme to HOR scroll ;
CGRect frame = _moreTable.frame;
_moreTable.transform = CGAffineTransformRotate(CGAffineTransformIdentity, k90DegreesCounterClockwiseAngle);
_moreTable.frame = frame;
[_moreTable registerNib:[UINib nibWithNibName:#"MoreTableViewCell" bundle:nil] forCellReuseIdentifier:#"moreCell"];
_moreTable.backgroundColor = [UIColor groupTableViewBackgroundColor];
_moreTable.delegate = self;
_moreTable.dataSource = self;
[_subView addSubview:_moreTable];
[_subView bringSubviewToFront:_moreTable];
The problem is when I click on table cell instead of executing didSelectRowAtIndexPath method he execute the subViewToggle Method. I tried to bring my tableView to front but didn't help
your tap gesture eat the touch. there are two way you can deal:
first, you can implement - gestureRecognizer:shouldReceiveTouch: delegate method, and decide if tap gesture should occur.
second, you also can set cancelsTouchesInView property of tap to NO, so tableView will receive touch. but tap will receive touch at the some time too.
Try implementing the UIGestureRecognizerDelegate protocol's gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
This should call both your didSelectRow and GestureRecognizer
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
Please try to use like this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view == _moreTable]) {
return NO;
}
return YES;
}
To solve this Problem I've changed the UIView Gesture from single finger tap to moveUP and moveDown gesture.
Note that I've tried only #dharmbir Solution and It haven't worked for me.
Update Also this solution, implementing the shouldReciveTouches, worked for the tapGesture and her's the code snippet :
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// UITableViewCellContentView => UITableViewCell
if([touch.view.superview isKindOfClass:[UITableViewCell class]]) {
return NO;
}
// UITableViewCellContentView => UITableViewCellScrollView => UITableViewCell
if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) {
return NO;
}
return YES;
}
//Create tap gesture for menu transparent view
UITapGestureRecognizer *rightTableTransparentViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(rightTableTransparentViewTapMethod:)];
[rightTableTransparentViewTap setCancelsTouchesInView:NO];
[_rightTableTransparentView addGestureRecognizer:rightTableTransparentViewTap];
- (void)rightTableTransparentViewTapMethod:(UITapGestureRecognizer *)recognizer {
//Write your code here
}
I want to implement , when swipe bottom to top of screen the keyboard is shown on screen and when swipe from top to bottom on screen the keyboard hides.
Its like iOS 7 effect when we swipe on screen the search textfield and keyboard is shown and when swipe down its hide.
Try this:
//declare a property to store your current responder
#property (nonatomic, assign) id currentResponder;
//in viewDidLoad:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(resignOnSwipe:)];
[self.collectionView addGestureRecognizer:swipe];
//Implement the below delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentResponder = textField;
}
//Implement resignOnSwipe:
- (void)resignOnSwipe:(id)sender {
[self.currentResponder resignFirstResponder]
}
Try Something like this,.. It works fine
In xib,
Add a textfield and hide it. Connect that to your .h file and name it as tf.
In .h file,
Add
#import <UIKit/UIKit.h>
#interface KeyboardDisplay : UIViewController <UIGestureRecognizerDelegate>
{
__weak IBOutlet UITextField *tf;
}
In .m file,
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture2];
}
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
[tf becomeFirstResponder];
NSLog(#"Up");
}
else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
{
[tf resignFirstResponder];
NSLog(#"down");
}
}
NOTE : Don't forget to hide the textfield in xib.
I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.
IBOutlet UITextView *myTextView;
And the method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
What I don't understand is how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)
Objective C:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
Swift:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
This is the best way I have found and it is very simple.
how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
Because you don't. You have to override this method on the view of which the text field is a subview.
What I do, is change the overall UIView class to UIControl.
This gives you a touchDown event you can link up to a method to resignFirstResponder.
The UIControl still gives you all the functionality of a UIView.
-(IBAction)backgroundTap:(id)sender
{
[text1 resignFirstResponder];
[text2 resignFirstResponder];
[textLogin resignFirstResponder];
[textPassword resignFirstResponder];
} // Resign all responders
- (void)viewDidLoad
{
//for keybord hide when touch outside:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(hidekeybord)];
[self.view addGestureRecognizer:tap];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)hidekeybord
{
[_yourtextfield.txt resignFirstResponder];
}
In .h
#property (nonatomic, assign) id currentResponder;
In .h
//in viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resignOnTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:singleTap];
//Implement the below delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentResponder = textField;
}
//Implement resignOnTap:
- (void)resignOnTap:(id)iSender {
[self.currentResponder resignFirstResponder];
}
Try the quick and dirty way:
Take a button and link it to an action method(lets call it Background). Stretch the button so it covers the whole view. Adjust the layers of the views so only things the user interacts by touch are on top of the button. Change the type of button to custom, this make the button invisible. Dismiss the firstResponder in the method Background.