Reputation: 7341
What is the cleanest way to detect taps on a UITableView background? I'd like to catch these to dismiss the keyboard.
Unfortunately, when I add a UITapGestureRecognizer to the tableview, tapping the cells fires the handler.
Upvotes: 2
Views: 1686
Reputation: 1631
Add the gesture in tableview's background view. Like this:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tableViewBackgroundTap)];
[self.tableView.backgroundView addGestureRecognizer:tapGesture];
Upvotes: 1
Reputation: 21593
In your UI(Table)ViewController or in your UITableView, override
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
and if the keyboard is being shown, dismiss it. Also, don't forget to forward this event to your subviews.
Upvotes: 2