9

I added a UITapGestureRecognizer to my main Content View in my ViewController to dismiss my keyboard when the content view is tapped.

The problem is that I have a UICollectionView inside my content view, and setting the UITapGestureRecognizer intercepts the taps of my UICollectionView.

How do I allow my UICollectionView's taps to go through so that the didSelectItemAtIndexPath method will fire again?

func setupGestureRecognizer() {
    let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    contentView.addGestureRecognizer(dismissKeyboardTap)
}

func dismissKeyboard() {
    contentView.endEditing(true)
}
The Nomad
  • 7,155
  • 14
  • 65
  • 100

2 Answers2

19

The way to solve this issue is by adding .cancelsTouchesInView = false to your UITapGestureRecognizer.

This allows touches inside other views to go through, such as a UITableViewCell touch.

func setupGestureRecognizer() {
    let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    dismissKeyboardTap.cancelsTouchesInView = false
    contentView.addGestureRecognizer(dismissKeyboardTap)
}

func dismissKeyboard() {
    contentView.endEditing(true)
}
The Nomad
  • 7,155
  • 14
  • 65
  • 100
0

try this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}  

and remove your tapGesture.

zangqilong
  • 21
  • 2
  • This stops the interception of the `UICollectionView` but does not dismiss the keyboard when tapping anywhere on the screen. – The Nomad Dec 27 '14 at 03:46