jonepatr jonepatr - 2 years ago 88
iOS Question

iphone, dismiss keyboard when touching outside of UITextField

I'm wondering how to make the keyboard disappear when the user touches outside of the UITextField?

Answer Source

You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.

The code:

In viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard {
       [aTextField resignFirstResponder];

(Where aTextField is the textfield that is responsible for the keyboard)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download