Dismiss keyboard with swipe gesture (as in Message app)

23,374

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:

enter image description here

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:

  1. Create an IBAction:

- (IBAction)dismiss:(id)sender;

  1. Go to your view in your xib and set the class for you view to UIControl.

  2. Drag and connect the UISwipeGestureRecognizer from the Library to your view.

  3. Connect the IBAction (TouchDown) with the UISwipeGestureRecognizer.

  4. Write the code to dismiss the keyboard:

    - (IBAction)dismiss:(id)sender 
    
    {
    
      [yourTextField resignFirstResponder];
    
    }
    

Done!

Share:
23,374
Michael Stet
Author by

Michael Stet

Updated on April 21, 2020

Comments

  • Michael Stet
    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
    Mundi over 12 years
    That 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
    JonasG over 12 years
    Well thats the only method that works to dismiss a keyboard with a swipe gesture that I know. Just wanted to help!
  • Filipe Pina
    Filipe Pina about 12 years
    simply awesome! (someone needs to accept this as the answer)
  • john.k.doe
    john.k.doe over 11 years
    agree 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
    john.k.doe over 11 years
    excellent, you should have more than 10 upvotes for that thing!
  • chris stamper
    chris stamper almost 10 years
    aaand it's broken in iOS 8. Guess it's time for everyone to find a new solution!
  • Bill
    Bill over 9 years
    This should be the accepted answer. Post-iOS 7, all you need is keyboardDismissMode.
  • Itesh
    Itesh over 9 years
    There are crashes in this, swizzled_addSubview. Dont even know how the crash is occuring
  • Gaby Fitcal
    Gaby Fitcal over 8 years
    I have a UIView which contains the textField. How can i do that?
  • ma11hew28
    ma11hew28 over 8 years
    @GabyFitcal That's fine. Read all the links above. I updated them.
  • Miguel Carvajal
    Miguel Carvajal about 8 years
    My answer below contains more details without providing a link that is subject to change in the future
  • Włodzimierz Woźniak
    Włodzimierz Woźniak over 7 years
    tableView.keyboardDismissMode = .OnDrag
  • bandejapaisa
    bandejapaisa about 7 years
    This 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
    Prashant Tukadiya almost 7 years
    @CanPoyrazoğlu Are able to find solution ? I am facing same
  • Can Poyrazoğlu
    Can Poyrazoğlu almost 7 years
    @MikeAlter nope, I've given up on it.
  • Prashant Tukadiya
    Prashant Tukadiya almost 7 years
    Ok @CanPoyrazoğlu if you are still looking for answer then see mattdipasquale 's Solution it is working ,