Reputation: 9054
How would I go about allowing a UITableView
to bounce when it reaches the bottom, but not the top. Essentially I'm adding a UIImageView
as my TableView's
section header, but currently if I pull up at the top there's an ugly space between my UIImageView
and my UINavigationBar
.
I look at this question but I don't really understand the solution. Would somebody mind elaborating?
Upvotes: 6
Views: 4089
Reputation: 1451
The contentOffset is the distance between the origin of your frame (0, 0) and the origin of the first cell.
Initially, the contentOffset is zero. If you've scrolled past the top of the list (dragging finger downwards), then the contentOffset will be negative. If you've scrolled down the list (first cell is above the frame), then the contentOffset will be positive.
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat maxOffset = (scrollView.contentSize.height - scrollView.frame.size.height);
CGFloat originOffset = 0;
// Don't scroll below the last cell.
if (scrollView.contentOffset.y >= maxOffset) {
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, maxOffset);
// Don't scroll above the first cell.
} else if (scrollView.contentOffset.y <= originOffset) {
scrollView.contentOffset = CGPointZero;
}
}
Upvotes: 4
Reputation: 119031
The question you link to has 2 different solutions. As the delegate of the table view you are already the scroll view delegate, so you could implement something like (this is the answer that isn't tagged as correct in the other question...):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
scrollView.bounces = (scrollView.contentOffset.y > 10);
}
Upvotes: 11