Dismissing the keyboard based on a touch of the content area of UITableViewController or UIController is actually pretty straight forward.
The basics are that you will add an action for tap gestures in the loadView
or viewDidLoad
events.
For example, if you have a TableViewController, you would add a the following code to the main tableView
for your UITableViewController:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
If this were a table, call addGestureRecognizer
on the main view
of your UIController
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
In both circumstances you need to create a method, called hideKeyboard
in this example, that will actually dismiss the keyaboard. It would look something like this:
- (void) hideKeyboard {
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}
Scroll Dismissal
You can also dismiss the keyboard when the user scrolls the table. You can do this with the UIScrollView property keyboardDismissMode
which has several options.
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
The above method property will dismiss the keyboard if scrolling interacts with the Keyboard.