Dismiss keyboard with swipe gesture

前端 未结 5 1412
清酒与你
清酒与你 2021-02-03 21:55

In Messages.app you can dismiss the keyboard down by scrolling the list view. To be clear, it isn\'t simply responding to a scrollViewDidScroll event. The keyboard

5条回答
  •  日久生厌
    2021-02-03 22:20

    Since iOS7, UIScroll​View and all classes that inherit from it (including UITableView) have a keyboard​Dismiss​Mode property. With Swift 5 and iOS 12, keyboard​Dismiss​Mode has the following declaration:

    var keyboardDismissMode: UIScrollView.KeyboardDismissMode { get set }
    

    The manner in which the keyboard is dismissed when a drag begins in the scroll view.

    Note that UIScrollView.KeyboardDismissMode is an enum that has none, interactive and onDrag cases.


    #1. Set keyboard​Dismiss​Mode programmatically

    The code snippet below shows a possible implementation of keyboardDismissMode:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Dismiss keyboard when scrolling the tableView
        tableView.keyboardDismissMode = UIScrollView.KeyboardDismissMode.interactive
    
        /* ... */
    }
    

    #2. Set keyboard​Dismiss​Mode in storyboard

    As an alternative to the programmatic approach above, you can set the keyboard​Dismiss​Mode value for your UIScrollView/UITableView in the storyboard.

    1. Select your UIScrollView / UITableView instance,
    2. Select the Attributes Inspector,
    3. Set the correct value for Keyboard.

提交回复
热议问题