6

There is similar question Stop UITableView over scroll at top & bottom?, but I need slighty different functionality. I want my table so that it can be overscrolled at the bottom, but cannot be overscrolled at the top. As I understood,

tableView.bounces = false

Allows to disable overscrolling at both top and bottom, however, I need disabling this only at the top. Like

tableView.bouncesAtTheTop = false
tableView.bouncesAtTheBottom = true
Community
  • 1
  • 1
caffeinum
  • 401
  • 5
  • 13

2 Answers2

12

For Swift 2.2, Use

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView == self.tableView {
        if scrollView.contentOffset.y <= 0 {
            scrollView.contentOffset = CGPoint.zero
        }
    }
}

For Objective C

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y<=0) {
        scrollView.contentOffset = CGPointZero;
    }
}
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Mohsin Qureshi
  • 1,203
  • 2
  • 16
  • 26
3

You can achieve it by changing the bounce property in scrollViewDidScroll of the tableView (you need to be the delegate of the tableView)

Have a property for the lastY:

var lastY: CGFloat = 0.0

Set initial bounce to false in viewDidLoad:

tableView.bounces = false

and:

func scrollViewDidScroll(scrollView: UIScrollView) {
    let currentY = scrollView.contentOffset.y
    let currentBottomY = scrollView.frame.size.height + currentY
    if currentY > lastY {
        //"scrolling down"
        tableView.bounces = true
    } else {
        //"scrolling up"
        // Check that we are not in bottom bounce
        if currentBottomY < scrollView.contentSize.height + scrollView.contentInset.bottom {
            tableView.bounces = false
        }
    }
    lastY = scrollView.contentOffset.y
}
oren
  • 3,159
  • 18
  • 33
  • You can just check if `scrollView.contentOffset.y` is smaller than 0.0. And if it is, just set it to 0.0. This way the scrollView won't be able to overscroll only in "up" direction – Maxim Skryabin May 17 '19 at 12:00