ipad/iphone: hide keyboard when user taps anywhere else on the screen
11,610
Solution 1
solution is here
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`
}
Comments
-
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 about 13 yearsfrom which method I can resign first responder
-
Ankit Sachan about 13 yearscontrol is not going to this method itself, tried putting break point in this
-
visakh7 about 13 yearsIs the view a viewController's view?
-
visakh7 about 13 yearsI think the tableview cells are absorbing the touch.
-
visakh7 about 13 yearsdid u try this method in the class of the custom table view?
-
Christopher Perry almost 13 yearsThis 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.