Dismiss keyboard with swipe gesture (as in Message

2019-03-08 07:45发布

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?

8条回答
倾城 Initia
2楼-- · 2019-03-08 08:08

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

查看更多
对你真心纯属浪费
3楼-- · 2019-03-08 08:15

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.

查看更多
不美不萌又怎样
4楼-- · 2019-03-08 08:23

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.

查看更多
Emotional °昔
5楼-- · 2019-03-08 08:23

The simplest solution is to set the following two properties:

Boom, baby!

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

查看更多
Explosion°爆炸
6楼-- · 2019-03-08 08:23

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!

查看更多
The star\"
7楼-- · 2019-03-08 08:26

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

Dismissing the keyboard in a UIScrollView

Hope that helps someone.

查看更多
登录 后发表回答