5

There are some answers out there such as this, but in the case where there is a UIScrollView or UICollectionView present, it doesn't work.
The touchesBegan method on the viewController will never get called.

On screen, I have a UITextField at the top.
Below that, filling up the rest of the screen is a UICollectionView.
I need to dismiss the keyboard if I touch anywhere besides the UITextField (including the collection view obviously)

So what is the best way to do this?

For such a common UI paradigm it seems like there should be a well-known solution, but I've yet to come across it.

Community
  • 1
  • 1
soleil
  • 12,133
  • 33
  • 112
  • 183

3 Answers3

4

To dismiss Keyboard on tap of the View: Add a Tap gesture to your ViewController.collectionView as follows:

//declare a property to store your current responder
@property (nonatomic, assign) id currentResponder;
//in viewDidLoad:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [self.collectionView addGestureRecognizer:singleTap];


//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnTap:

- (void)resignOnTap:(id)sender {
    [self.currentResponder resignFirstResponder];
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • This would work if the user taps the collection view, but I wish there was a more general solution for any touch outside the keyboard. – soleil Jan 06 '14 at 03:29
  • I don't think there is any other easy way around. any how you have to implement gesture recognition here. Let me know if my answer is useful ..:) – Prince Agrawal Jan 06 '14 at 03:36
1

The simple way to do it is:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES]; 
}
Opal
  • 81,889
  • 28
  • 189
  • 210
Bisca
  • 6,380
  • 2
  • 19
  • 32
1

Here's a better solution which doesn't require adding gesture recognisers individually to everything. It's in Swift, but could easily be converted to ObjC.

Add the following to your viewDidLoad():

let tap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)

and then add the following method declaration:

func dismissKeyboard()
{
    view.endEditing(true)
}

...where view is your text field.

HughHughTeotl
  • 5,439
  • 3
  • 34
  • 49