Eric Eric - 6 months ago 23
iOS Question

How to hide keyboard on touch UITableView in iOS Obj-C

I am new in iOS development. I want to hide the keyboard when tapping outside of a

TextField
. My
TextField
is in a cell from an
UITableView
.

I have tried to follow some of those links, however without any success--

Dismiss keyboard on touch anywhere outside UITextField

Dismiss keyboard by touching background of UITableView

Hide keyboard when scroll UITableView

I am trying to find the simplest way possible.
Thanks in advance

Answer

This is the simplest way to dismiss keyboard

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
    [self.view endEditing:YES];
}
Comments