Frage

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

War es hilfreich?

Lösung

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;
    }
}

Andere Tipps

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.
            }
        }
    }
}

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.

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.

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

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
     }
        
}

In swift 2.2:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y <= 0 {
        scrollView.contentOffset = CGPointZero
    }
}
-(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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top