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 tracks with your finger as you swipe down. Any idea how this is done?
32
votes
5 Answers
51
votes
13
votes
6
votes
2
votes
Since iOS7, UIScrollView
and all classes that inherit from it (including UITableView
) have a keyboardDismissMode
property. With Swift 5 and iOS 12, keyboardDismissMode
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 keyboardDismissMode
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 keyboardDismissMode
in storyboard
As an alternative to the programmatic approach above, you can set the keyboardDismissMode
value for your UIScrollView
/UITableView
in the storyboard.
- Select your
UIScrollView
/UITableView
instance, - Select the Attributes Inspector,
- Set the correct value for Keyboard.