user3766930
user3766930

Reputation: 5829

I know how to disable bouncing of uitableview on top only - but how can I do it only on the bottom?

I managed to disable bouncing effect when user scrolls to the top. I would like to reverse it somehow so that the bouncing effect is disabled on the bottom. Could you help me with modifying my code for that purpose?

var lastY: CGFloat = 0.0

func scrollViewDidScroll(scrollView: UIScrollView) {
    let currentY = scrollView.contentOffset.y
    let currentBottomY = scrollView.frame.size.height + currentY
    if currentY > lastY {
        //"scrolling down"
        tableView.bounces = true
    } else {
        //"scrolling up"
        // Check that we are not in bottom bounce
        if currentBottomY < scrollView.contentSize.height + scrollView.contentInset.bottom {
            tableView.bounces = false
        }
    }
    lastY = scrollView.contentOffset.y
}

Upvotes: 2

Views: 8539

Answers (3)

KaMaHe
KaMaHe

Reputation: 473

Try this:

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if scrollView == tableView {
                if (self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height))
                {
                    scrollView.contentOffset = CGPoint(x: 0, y: (self.tableView.contentSize.height - self.tableView.bounds.size.height))
                }
            }
        }

Upvotes: 3

guru
guru

Reputation: 2817

You can try this code

override func scrollViewDidScroll(scrollView: UIScrollView)

{
   if scrollView.contentOffset.y <= 0  
     {
        scrollView.contentOffset = CGPointZero
     }
}

Upvotes: 6

Wolverine
Wolverine

Reputation: 4329

Did you try setting bounce and VerticalBounce property to NO.

You can set these properties programatically like

In Swift

tableView.bounces = false
tableView.alwaysBounceVertical = false

In Objective-C

tableView.bounces = NO;
tableView.alwaysBounceVertical = NO;

OR

if you want to do it from Storyboard. Make you tableview setting like below :

enter image description here

Upvotes: 6

Related Questions