6

I can show keyboard by using textField.becomeFirstResponder() or can hide keyboard by using textField.resignFirstResponder().

You may notice in iPhone default message app, when we scroll (or drag) to bottom, the keyboard hides smoothly. And even if we start scroll (or drag) to upside by not touching the bottom, the keyboard shows dynamically.

If I have scrollview or user drag to bottom then how can I implement that.

enter image description here

Community
  • 1
  • 1
Litle Dev
  • 474
  • 1
  • 5
  • 18
  • Possible duplicate of [UIScrollView - Move keyboard with touch drag like iOS 7 mail app?](https://stackoverflow.com/questions/21335103/uiscrollview-move-keyboard-with-touch-drag-like-ios-7-mail-app) – nschum Aug 14 '17 at 23:52

3 Answers3

12

If you have a UIScrollView (or a UITableView/UICollectionView since they inherit from UIScrollView) you can simply set keyboardDismissMode property to interactive.

Objective-C :
self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

Swift
self.scrollView.keyboardDismissMode = .interactive

As usual, more in the docs.

Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • How are they able to start the dismissal at the top of the textfield and not at the top of the keyboard? Is there anyway to account for that extra height? – Stefan Dec 30 '18 at 00:38
  • 2
    @Stefan you set the textfield as the `inputAccessoryView` of the view controller and make the view controller first responder by overriding `canBeFirstResponder` and returning `true` – Exception Jan 15 '19 at 13:02
1

Easy way to dismiss keyboard when touch or drag

YanSte
  • 10,661
  • 3
  • 57
  • 53
0

for any object inherit from UIScrollView you can either to set (Dismiss on drag) for Keyboard option from the Attributes inspector or to use below code for swift:

self.scrollView.keyboardDismissMode = .interactive
Let.Simoo
  • 93
  • 9