Apollo
Apollo

Reputation: 9054

enable bouncing for only one direction UITableView

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

Answers (2)

iamreptar
iamreptar

Reputation: 1451

The contentOffset is the distance between the origin of your frame (0, 0) and the origin of the first cell.

iOS: The default coordinate system has its origin at the upper left of the drawing area, and positive values extend down and to the right from it. You cannot change the default orientation of a view’s coordinate system in iOS—that is, you cannot “flip” it.

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

Wain
Wain

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

Related Questions