Dismiss keyboard with swipe gesture (as in Message app)
Asked Answered
P

8

36

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?

Polston answered 20/8, 2011 at 17:38 Comment(5)
It does not do that on my iPhone. (3GS, iOS 4.3.3)Tobias
Ah, this may hypothetically be in a future OS release then. I would like to discuss how such an effect could be obtained, in principle.Polston
I believe it is an iOS 5.x feature. Should be out of NDA by now.Eke
Sadly, this functionality still hasn't appeared in iOS 5.x. Check out my answer for an open source attempt.Chromatism
@DanielAmitay I found a great solution for iOS 7.1+.Pardon
C
44

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.

Chromatism answered 19/2, 2012 at 22:29 Comment(7)
simply awesome! (someone needs to accept this as the answer)Sandlin
excellent, you should have more than 10 upvotes for that thing!Calculated
aaand it's broken in iOS 8. Guess it's time for everyone to find a new solution!Acro
There are crashes in this, swizzled_addSubview. Dont even know how the crash is occuringFayfayal
@CanPoyrazoğlu Are able to find solution ? I am facing sameAerology
@MikeAlter nope, I've given up on it.Howund
Ok @CanPoyrazoğlu if you are still looking for answer then see mattdipasquale 's Solution it is working ,Aerology
P
34

The simplest solution is to set the following two properties:

Boom, baby!

Check out Acani Chats iPhone Client ChatViewController.swift for an example.

Pardon answered 23/6, 2014 at 5:28 Comment(4)
This should be the accepted answer. Post-iOS 7, all you need is keyboardDismissMode.Rebuff
I have a UIView which contains the textField. How can i do that?Flashgun
@GabyFitcal That's fine. Read all the links above. I updated them.Pardon
tableView.keyboardDismissMode = .OnDragGamal
O
30

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

Otti answered 23/1, 2016 at 14:34 Comment(1)
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.Cracknel
W
10

In iOS 7, you can now dismiss the keyboard interactively on a UIScrollView.

Dismissing the keyboard in a UIScrollView

Hope that helps someone.

Whipple answered 29/11, 2013 at 1:55 Comment(1)
My answer below contains more details without providing a link that is subject to change in the futureOtti
R
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!

Reliquary answered 20/8, 2011 at 17:46 Comment(3)
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.Tobias
Well thats the only method that works to dismiss a keyboard with a swipe gesture that I know. Just wanted to help!Reliquary
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).Calculated
W
1

In swift you can use below code to get the view container of current keyboard (if exist), than you can change is's frame in your code:

UIApplication.shared.windows
    .filter{ NSStringFromClass($0.classForCoder) == "UIRemoteKeyboardWindow" }
    .first?.subviews.filter { NSStringFromClass($0.classForCoder) == "UIInputSetContainerView" }
    .first?.subviews.filter { NSStringFromClass($0.classForCoder) == "UIInputSetHostView" }
    .first

By the way here are a tool called Reveal make you can see the view hierarchical.

Walleye answered 20/4, 2017 at 3:30 Comment(0)
S
0

I think the best way is to place a hidden button above the text input container. a long strip and when it detects a touchdown and release or cancel then hide the keyboard.

I'm going to try it and i will let you know how i go.

Safekeeping answered 29/9, 2011 at 0:46 Comment(0)
E
0

Short answer; They're most likely doing some 'Private API' thing there.

I'm most sure that the keyboard is in an independent view, above your app's window (You do not have access/control to it and it always displays on top, no matter what). The most you can do is have the input view become/resign first responder status, and the keyboard will appear/disappear accordingly: All or nothing.

You may be able to get a handle of the keyboard view and change its frame property (using undocumented properties of documented classes, and undocumented classes), but I'm pretty sure that will get you kicked out of the store.

Eke answered 29/9, 2011 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.