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
If you're using a tableView
and Swift 3 or Swift 4, it works by using:
tableView.keyboardDismissMode = .onDrag
Since iOS 7, you can use
scrollView.keyboardDismissMode = .Interactive
From the documentation:
UIScrollViewKeyboardDismissModeInteractive
The keyboard follows the dragging touch offscreen, and can be pulled upward again to cancel the dismiss.
Without tableview - yes it not a swipe but it doesn't the trick
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
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.
keyboardDismissMode
programmaticallyThe 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
/* ... */
}
keyboardDismissMode
in storyboardAs an alternative to the programmatic approach above, you can set the keyboardDismissMode
value for your UIScrollView
/UITableView
in the storyboard.
UIScrollView
/ UITableView
instance,In the XCode, attributes inspector, the scrollView
has a Keyboard
attribute. It has 3 options.