Luther Baker
Luther Baker

Reputation: 7341

Detecting taps on UITableView background

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

Answers (2)

inexcii
inexcii

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

0xSina
0xSina

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

Related Questions