Stop overscroll UITableView only at the top?
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;
}
}
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
}