Reputation: 13838
Here's my situation: I've got a UISearchBar and a UITableView in a Navigation Controller. When the user taps in the search bar, the keyboard pops so the user can type a search string, as shown here:
But the keyboard obscures the list, and so I want to make it so that if the user taps the table, the keyboard goes away, and I'm having a surprising amount of difficulty.
What I want is something like a TouchDownInside event on the table itself, so I can dismiss the keyboard when the table gets user input of any kind, but this doesn't seem to exist. What's the best way to do this?
Upvotes: 0
Views: 85
Reputation: 69027
If I use a TapGestureRecognizer, I can tell if the user taps anywhere, but then when they tap on the table, the normal table events don't work.
You could try and set cancelsTouchesInView
to NO for your tap gesture recogniser:
tapGestureRecognizer.cancelsTouchesInView = NO;
This should allow the touches to be forwarded to the view even when the gesture is recognized.
You might also need to define a delegate for your gesture recognizer and the following method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This is assuming that the table also manages its gestures through a gesture recognizer and thus to allow that both can work simultaneously.
Upvotes: 1
Reputation: 3526
An easy solution would be to catch when the UITableView is scrolling, as the UITableView extends the UIScrollView you can look for the scrollViewDidScroll: and make the search bar resign the first responder to make the keyboard go away, same can be done with scrollViewWillBeginDragging: method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[searchBar resignFirstResponder];
}
Implement this on you UITableViewDelegate as it behaves as the delegate of your UITableView
More information at
http://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollviewdelegate_protocol/
Upvotes: 1