I am having trouble getting the keyboard in my iPhone app to go away because the UIView
even when made a controller is not touchable because of the fact that I have a UITableView
taking up the rest of the available screen.
I was curious to know how I would go resigning the keyboard aka firstResponder
by clicking onto the UITableView
? Is there a way to monitor a touch event on the UITableView
even if it is not to select a clickable cell.
Basically, I know how to resign the keyboard if the cell fires the event but, if I click on a non - clickable part of the UITableView
I would still like the keyboard to go away.
2 options:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView { [self.view endEditing:YES]; }
Personally I usually do it on table scroll, since I don't like a single tap to dismiss the keyboard.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 1;
[self.tableView addGestureRecognizer:doubleTap];
[doubleTap release];
}
- (IBAction)tapDetected:(UIGestureRecognizer *)sender
{
CGPoint p = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if(indexPath == nil)
{
NSLog(@"empty");
}
else
{
[textField resignFirstResponder];
}
}
I think it will help... try it..
Adding a tap gesture recognizer is an interesting solution, but there's an alternative and you don't need to code anything!
You can set in Interface Builder the property keyboardDismissMode to "Dismiss on drag" for your table view. It's a property inherited from UIScrollView and whenever you scroll your table view, the keyboard is dismissed.
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