Dismiss keyboard with swipe gesture (as in Message app)

依然范特西╮ 提交于 2019-12-02 15:50:06

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.

ma11hew28

The simplest solution is to set the following two properties:

Boom, baby!

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

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

Jeremy Massel

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

Dismissing the keyboard in a UIScrollView

Hope that helps someone.

JonasG

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!

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!