27

I was wondering how to detect if the UITableView is scrolled (up or down). I want to hide the keyboard when the UITableView is scrolled with self.view.endEditing(true).

Thanks in advance

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
Kaaseter
  • 359
  • 1
  • 5
  • 6
  • Seem to be a [XY-Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) as OP assumes that some scrollview delegate method must be implemented to dismiss the keyboard. but as Daniil [shows](http://stackoverflow.com/a/40306149/106435), there is a simpler solution for that. The question would be better phrased "*how to dismiss keyboard on scroll*" – vikingosegundo Oct 28 '16 at 16:20

4 Answers4

53

You can set property of UITable view (XCode 7+)

In Storyboard:
In Storyboard

in Code:

tableView.keyboardDismissMode = .onDrag
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Daniil Chuiko
  • 866
  • 8
  • 7
40

You can add UIScrollViewDelegate. After that you can implement scrollViewDidScroll method.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
12

I believe the complete solution would be the following:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == feedTableView {
        let contentOffset = scrollView.contentOffset.y
        print("contentOffset: ", contentOffset)
        if (contentOffset > self.lastKnowContentOfsset) {
            print("scrolling Down")
            print("dragging Up")
        } else {
            print("scrolling Up")
            print("dragging Down")
        }
    }
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if scrollView == feedTableView {
        self.lastKnowContentOfsset = scrollView.contentOffset.y
        print("lastKnowContentOfsset: ", scrollView.contentOffset.y)
    }
}

The previous answers weren't 100% accurate.

Explanation: scrollViewDidEndDragging will be called when the scrolling stops, therefore we save the last know offset. After that we compare it with the current offset in the delegate method scrollViewDidScroll.

Giggs
  • 851
  • 10
  • 16
  • it's the last known offset position of the scrollview, used to compare with the new position. It'll allow you to identify the movement. As seen in the scrollViewDidScroll @Dilip Tiwari – Giggs Aug 23 '17 at 07:58
  • could u help me in resolving one issue related to this @Giggs – Dilip Tiwari Aug 23 '17 at 07:59
  • i am facing issue with tableview scrolling @Giggs – Dilip Tiwari Aug 23 '17 at 08:01
  • What's your issue? @Dilip Tiwari – Giggs Aug 23 '17 at 08:01
  • I have a question,In my view controller i have dragged a view just above tableviewcell inside tableview which contains 3 textfields,above tableview i have taken a collection view.my question is how that when i scroll the whole view controller to top i want to hide collection view as well as that which which contains those 3 textfields.And also create one new textfield and also position of tableview will be just below new textfield and when scroll the view controller down then that new textfield will hide and other things again show with same position as it is before scrolling.@Giggs – Dilip Tiwari Aug 23 '17 at 08:04
  • I didn't really understand your problems, but i can advise you to use the tableView header // footer for such purposes and the header can be told to scroll or stay static. I need to see the UI (design) to properly advise you how to develop it. @Dilip Tiwari – Giggs Aug 23 '17 at 08:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152609/discussion-between-dilip-tiwari-and-giggs). – Dilip Tiwari Aug 23 '17 at 09:05
  • Anyway you can show us how to get or set lastKnowContentOfset? @Giggs – Joe Sene Aug 31 '17 at 13:58
8
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        if(velocity.y>0){
            NSLog("dragging Up");
        }else{
            NSLog("dragging Down");
        }
    }
Mick
  • 30,759
  • 16
  • 111
  • 130
Jamil
  • 2,977
  • 1
  • 13
  • 23