Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 342
Apollo Avatar asked Aug 01 '13 20:08

Apollo


2 Answers

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);
}
like image 113
Wain Avatar answered Nov 15 '22 11:11

Wain


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;
    }
}
like image 24
iamreptar Avatar answered Nov 15 '22 10:11

iamreptar