aaisataev
aaisataev

Reputation: 1683

How can I refresh the table view but disable the bounce?

I want to disable my scrollview bounce when scroll down.

enter image description here

When I disable bounce vertically I can't refresh my table. Any suggestion how to disable bounce, but enable refresh table?

I'm refreshing this way:

self.refreshControl = UIRefreshControl()
    self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refreshControl)

func refresh(sender:AnyObject) {
    getJson()
    self.tableView.reloadData()
    self.refreshControl.endRefreshing()
}

Thanks.

Upvotes: 5

Views: 3685

Answers (2)

craft
craft

Reputation: 2135

Implement the scrollViewDidScroll method in the UIScrollViewDelegate

  extension ViewController: UIScrollViewDelegate {
      func scrollViewDidScroll(_ scrollView: UIScrollView) {
          if scrollView.contentOffset.y > 0 {
              scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: 0), animated: false)
          }
      }
    }

This allows the table view to pull to refresh down, but restricts pulling up. This removes the bounce in the upwards direction.

Side-effect: this will not let you go beyond the cells already on view, so you can't scroll down to cells not visible.

Upvotes: 0

aaisataev
aaisataev

Reputation: 1683

Just did it this way:

func scrollViewDidScroll(scrollView: UIScrollView) {

    if scrollView.contentOffset.y < 0.0 {
        return
    }

    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
        scrollView.setContentOffset(CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height - scrollView.frame.size.height), animated: false)
    }
}

Upvotes: 9

Related Questions