11

I am using scrollViewDidScroll delegate in my application.

But, many times, even though I dint start scrolling, this delegate is getting invoked which is creating a lot of problem. I heard that even when contentSize for a particular scroll view is set then at that time also this delegate i.e., scrollViewDidScroll will invoke.

What are the different scenarios in which this delegate gets invoked. What are the steps to control this?

Can I set any parameter to handle this?

Bharath
  • 3,001
  • 6
  • 32
  • 65

5 Answers5

10

To prevent scrollDidScroll: from firing automatically when the view is loaded and adjusted, I waited to add my UIScrollView delegate until after all the views load using viewDidLayoutSubviews. It is working well for me.

- (void)viewDidLayoutSubviews {
    // add table view delegate after the views have been laid out to prevent scrollViewDidScroll
    // from firing automaticly when the view is adjusted on load, which makes the tab bar disappear 
    self.tableView.delegate = self;
} 
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Sev
  • 883
  • 1
  • 14
  • 34
5

relevant

scrollViewDidScroll: gets called every time the scroll bounds change. This means it gets called during the scroll, as well as when it starts. You may want to try scrollViewWillBeginDragging: instead.

Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
5

scrollViewDidScroll also gets invoked when the orientation changes. This I came to know from here. This was the problem I was facing. And now my problem solved with this post.

Community
  • 1
  • 1
Bharath
  • 3,001
  • 6
  • 32
  • 65
4

If you want to know if scrollDidScroll was triggered manually (through finger gesters) or due to other events like didSelect or setContentOffset, use the UIScrollView.isTracking and UIScrollView.isDecelerating properties.

Example usage:

if scrollView.isTracking || scrollView.isDecelerating {
    scrollPosition = collectionView.contentOffset
}
James Risner
  • 5,451
  • 11
  • 25
  • 47
1

Set UICollectionView, UITableView delegate here in this method

override func viewDidLayoutSubviews() {
     super.viewDidLayoutSubviews()
     // This method is called only after all subviews are laid
}
Karthik damodara
  • 1,805
  • 19
  • 17