Pages

Thursday, February 28, 2013

Hide the keyboard when you touch outside of an UITextField in iOS

On viewDidLoad add the following lines:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];


Then add the following function somewhere in the implementation file.

- (void) hideKeyboard {
[activeField resignFirstResponder];
}

Here activeField is an UITextField object. Declare it somewhere at the top of the view controller's implementation file.

No comments:

Post a Comment