How can I dismiss the keyboard if a user taps off the on-screen keyboard?
Solution 1
You'll need to add an UITapGestureRecogniser
and assign it to the view, and then call resign first responder on the textfield on it's selector.
The code:
In viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
(Where aTextField is the textfield that is responsible for the keyboard)
OPTION 2
If you can't afford to add a gestureRecognizer then you can try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
Solution 2
The simplest solution I have used is this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
The endEditing command can be used on any view that contains your textfield as a subview. The other advantage of this method is that you don't need to know which textfield triggered the keyboard. So even if you have a multiple textfields, just add this line to the superview.
Based on the Apple documentation, I think this method exists specifically to solve this problem.
Solution 3
Add a tapGesture Recognizer but make sure cancelsTouchesInView = NO
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
Solution 4
You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.
The code:
In viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
-(void)dismissKeyboard {
[self.view endEditing:true];
}
Solution 5
This is a Swift 4 solution:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
self.view.addGestureRecognizer(tap)
And the dismissKeyboard
@objc func dismissKeyboard() {
self.view.endEditing(true)
}
Sheehan Alam
iOS, Android and Mac Developer. i can divide by zero.
Updated on July 09, 2022Comments
-
Sheehan Alam almost 2 years
I want to be able to dismiss the iPhone keyboard when the user taps anywhere outside of the keyboard. How can I go about doing this? I know I need to dismiss the responder, but need to know how to implement it when a user taps out of the keyboard space.
-
Sheehan Alam about 13 yearsvery elegant solution w/ the gesture recognizer!
-
kmehta about 13 yearsThis is great, however, the gesture action is eating all my button click events on the view. Any ideas?
-
IOS Rocks about 11 yearsGood solution for dismissing the keyboard in ios.
-
Jim True about 11 yearsI thought this was great but it overrides any other inputs like CollectionViews too. See Matt Rees' comment below for fix.
-
Miros about 10 yearsExtremely good point about the tapGesture.cancelsTouchesInView = NO; Thanks a bunch!
-
testing over 9 yearsWhat about a
UIScrollView
? -
StackUnderflow about 7 yearsThis is the best solution no DOUBT!
-
Vadim Bulavin over 6 yearsInstead of
[touches anyObject]
usetouches.first
in Swift. -
Reza Dehnavi over 5 yearsTry this for UIScrollView
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;