ipad/iphone: hide keyboard when user taps anywhere else on the screen

11,610

Solution 1

solution is here

http://www.mobisoftinfotech.com/blog/iphone/iphone-uitextfield-tutorial-handling-keyboard-interactions/

for tableView you can all the same method in didSelectRow method

Solution 2

Here's a pretty elegant solution of this task:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
}
- (void)dismissKeyboard
{
   [searchBar resignFirstResponder];
}

Solution 3

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

// dismiss keyboard through `resignFirstResponder`

}
Share:
11,610
Ankit Sachan
Author by

Ankit Sachan

:-)

Updated on June 05, 2022

Comments

  • Ankit Sachan
    Ankit Sachan almost 2 years

    I have a situation,

    I have some searchbar on same screen when i click on a search bar keyboard appears. Now is it possible that this keyboard disappears if we click any where else on the scree.

    Please help me with this.

    Thanx in advance.

  • Ankit Sachan
    Ankit Sachan about 13 years
    from which method I can resign first responder
  • Ankit Sachan
    Ankit Sachan about 13 years
    control is not going to this method itself, tried putting break point in this
  • visakh7
    visakh7 about 13 years
    Is the view a viewController's view?
  • visakh7
    visakh7 about 13 years
    I think the tableview cells are absorbing the touch.
  • visakh7
    visakh7 about 13 years
    did u try this method in the class of the custom table view?
  • Christopher Perry
    Christopher Perry almost 13 years
    This doesn't work if you are tapping inside a UIScrollView. For that case you should subclass UIScrollView and override the - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method and resignFirstResponder on whatever the first responder is.