Dismiss keyboard with swipe gesture (as in Message app)
Solution 1
I created a UIView category that provides the desired functionality:
https://github.com/danielamitay/DAKeyboardControl
Edit: It has indeed been used on the app store.
Solution 2
The simplest solution is to set the following two properties:
Boom, baby!
Check out Acani Chats iPhone Client ChatViewController.swift
for an example.
Solution 3
Luckily, Apple added the handy property keyboardDismissMode
on UIScrollView
to make your life a little easier.
Now your app can behave like Messages.app just by changing a single property on your Storyboard, or alternatively by adding one line of code!
This property uses the new UIScrollViewKeyboardDismissMode
enum. The possible values of this enum are as follows:
UIScrollViewKeyboardDismissModeNone // the keyboard is not dismissed automatically when scrolling
UIScrollViewKeyboardDismissModeOnDrag // dismisses the keyboard when a drag begins
UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
Here’s the Storyboard property to change to dismiss the keyboard on scroll:
Hope that helps solving your problem
Solution 4
In iOS 7, you can now dismiss the keyboard interactively on a UIScrollView.
Dismissing the keyboard in a UIScrollView
Hope that helps someone.
Solution 5
You can use UISwipeGestureRecognizer to do so. I don't know how to implement it using code, but if you are using the new xcode 4.2 beta then heres an easy method:
- Create an IBAction:
- (IBAction)dismiss:(id)sender;
Go to your view in your xib and set the class for you view to UIControl.
Drag and connect the UISwipeGestureRecognizer from the Library to your view.
Connect the IBAction (TouchDown) with the UISwipeGestureRecognizer.
-
Write the code to dismiss the keyboard:
- (IBAction)dismiss:(id)sender { [yourTextField resignFirstResponder]; }
Done!
Michael Stet
Updated on April 21, 2020Comments
-
Michael Stet about 4 years
When the keyboard is showing on the iPhone's Messages app, if the user begins a swipe down from the messages tableview and continues into the keyboard area, the keyboard will begin to dismiss. If they move their finger up and down during this process, the keyboard moves with it.
Are Apple doing this with private APIs, or is there a way to control the keyboard like this from (I presume) a gesture recognizer?
-
Mundi over 12 yearsThat would not work. The keyboard would slide down and disappear, but not come back up when the finger moves up again before the gesture is finished.
-
JonasG over 12 yearsWell thats the only method that works to dismiss a keyboard with a swipe gesture that I know. Just wanted to help!
-
Filipe Pina about 12 yearssimply awesome! (someone needs to accept this as the answer)
-
john.k.doe over 11 yearsagree with JonasG. whereas Apple is probably using a form of UIPanGestureRecognizer under the hood, it requires their own internal knowledge of controlling the frame of the keyboard to get it to slide up and down in the fashion you see in the Messages.app . the closest thing would be to use a UISwipeGestureRecognizer (or perhaps a UIPanGestureRecognizer, though it would act virtually the same as a swipe in that the down would start the motion and there would be no way to arrest the keyboard from disappearing at that point).
-
john.k.doe over 11 yearsexcellent, you should have more than 10 upvotes for that thing!
-
chris stamper almost 10 yearsaaand it's broken in iOS 8. Guess it's time for everyone to find a new solution!
-
Bill over 9 yearsThis should be the accepted answer. Post-iOS 7, all you need is
keyboardDismissMode
. -
Itesh over 9 yearsThere are crashes in this, swizzled_addSubview. Dont even know how the crash is occuring
-
Gaby Fitcal over 8 yearsI have a UIView which contains the textField. How can i do that?
-
ma11hew28 over 8 years@GabyFitcal That's fine. Read all the links above. I updated them.
-
Miguel Carvajal about 8 yearsMy answer below contains more details without providing a link that is subject to change in the future
-
Włodzimierz Woźniak over 7 yearstableView.keyboardDismissMode = .OnDrag
-
bandejapaisa about 7 yearsThis is great. Got excited, I have nothing to do but check a box. Then realised my input view attached to the top of my keyboard didn't move with it...doh.
-
Prashant Tukadiya almost 7 years@CanPoyrazoğlu Are able to find solution ? I am facing same
-
Can Poyrazoğlu almost 7 years@MikeAlter nope, I've given up on it.
-
Prashant Tukadiya almost 7 yearsOk @CanPoyrazoğlu if you are still looking for answer then see mattdipasquale 's Solution it is working ,