13

Is it possible to bounce a UITableView on the bottom side, but not the top? If so, please show me code.

shadow of arman
  • 395
  • 1
  • 7
  • 16
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185

8 Answers8

44

Instead of changing the bounces property, I added that to the UIScrollViewDelgate method:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y<=0) {
        scrollView.contentOffset = CGPointZero;
    }
}
Liron Berger
  • 456
  • 5
  • 3
  • This will prevent scrolling to top completely – DarkLeafyGreen Apr 12 '15 at 17:06
  • It would be better to set `bounds` rather than `contentOffset` to avoid calling the delegate a second time, especially if there is more code in `scrollViewDidScroll:`. – Arkku Oct 22 '15 at 11:03
8

For swift 4

extension YourViewController: UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == tableView {
            let a = scrollView.contentOffset
            if a.y <= 0 {
                scrollView.contentOffset = CGPoint.zero // this is to disable tableview bouncing at top.
            }
        }
    }
}
abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
4

It should be possible by observing UITableview's contentOffset property.

[_tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];

. . and preventing the offset from going negative.

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
Nazik
  • 8,696
  • 27
  • 77
  • 123
  • 1
    UITableView is a sub-class of UIScrollView. . . this gives me an idea. . . observe content offset. . if it goes negative, lock back to zero. – Jasper Blues Jun 18 '13 at 11:48
4

I have also encountered this problem and my solution is:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
 if (scrollView.contentOffset.y > 100) {
     scrollView.bounces = NO;
 }else{
     scrollView.bounces = YES;
 }
}

I use this code in my project and it work well.

无夜之星辰
  • 5,426
  • 4
  • 25
  • 48
2

You could try using the UIScrollViewDelegate methods to detect dragging direction and adjust the bounces property appropriately?

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
  • Hi i tried observing and changed the tableview.bounces to NO accordingly when offset reaches negative but there is glitch when the tableview goes in negative. How did you avoid it , please share me your solution if it did'nt occur to you. – Mohammed Shahid Apr 10 '14 at 09:47
1

Swift

func scrollViewDidScroll(_ scrollView: UIScrollView) {

     //Check to make sure this only applies to tableview 
     //and not other scrollviews inside your View/ViewController
     if scrollView.superclass == UITableView.self {
     //bounce will only be true if contentOffset.y is higher than 100
     //I set 100 just to make SURE, but you can set >= 0 too
         scrollView.bounces = scrollView.contentOffset.y > 100
     }
        
}

shadow of arman
  • 395
  • 1
  • 7
  • 16
0

In swift 2.2:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y <= 0 {
        scrollView.contentOffset = CGPointZero
    }
}
Jelly
  • 4,522
  • 6
  • 26
  • 42
0
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= 0) {
        scrollView.contentOffset = CGPointZero;
        scrollView.showsVerticalScrollIndicator = NO;
    } else {
        scrollView.showsVerticalScrollIndicator = YES;
    }
}

@Liron Berger 's solution will cause VerticalScrollIndicator still showing, just set showsVerticalScrollIndicator to NO can solve this problem

likid1412
  • 963
  • 8
  • 15