Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide keyboard on touch anywhere outside UITextField when view includes UICollectionView

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.

like image 948
soleil Avatar asked Jan 05 '14 18:01

soleil


People also ask

How to dismiss the keyboard in a uitextfield?

Drop in a UITextField (we will omit UITextView from now on, but it’s the same), start editing, and you will soon need to learn how to dismiss the keyboard. The code to dismiss is simple: The difficulty is where to put this code. There are 2 scenarios - using a scroll view, or not. 1. Using a scroll view

How to hide the keyboard on tap outside in React Native?

Hide the keyboard on tap outside : Keyboard handling is easy in React Native. It provides one module called Keyboard for keyboard related stuff. Just import that module and call it’s dismiss method to hide a keyboard.

What are the two textview functions used in UITextView?

We shall use 2 textView functions to control their behaviour: textViewDidBeginEditing (..) and textViewDidEndEditing (..). We shall separate the code of UITextFields and UITextView to make it easier to focus on each group keyboard control.

How do I Turn Off touch events on a scroll view?

In the keyboard property, select “Dismiss on drag”. This will dismiss when you drag/scroll the scroll view. In using tap gesture recognizer, you might want to disable cancelsTouchesInView (set to NO), so that it pass the touch event to other views (eg table view cell).


2 Answers

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];
}
like image 83
Prince Agrawal Avatar answered Sep 27 '22 20:09

Prince Agrawal


The simple way to do it is:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES]; 
}
like image 22
Bisca Avatar answered Sep 27 '22 20:09

Bisca