What do I have to do after setting contentInset or automaticallyAdjustsScrollViewInsets for it to actually beeing applied?

StackOverflow https://stackoverflow.com/questions/22598865

Question

I have got an UIViewController containing an UIScrollView. Initially automaticallyAdjustsScrollViewInsets is not set (so it should default to YES).

On some user interaction I set automaticallyAdjustsScrollViewInsets to NO, so I can set my own contentInset on the scroll view.

After doing so nothing happens. What do I have to call afterwards?

Calling setNeedsDisplay on the scroll view doesn’t help.

Was it helpful?

Solution

If you want your initial display of your scrollview to include an initial content offset, then set the contentInset value in viewDidLoad. If you wish to dynamically change the contentInset and have the contentOffset change as well, then you need to adjust the contentOffset at the same time as the contentInset. For example:

// Adjust content inset and initial offset to account for the header
UIEdgeInsets contentInset = self.collectionView.contentInset;
contentInset.top += self.stickyHeaderView.bounds.size.height;
self.collectionView.contentInset = contentInset;
if (-1 * self.collectionView.contentOffset.y < contentInset.top) {
    CGPoint contentOffset = self.collectionView.contentOffset;
    contentOffset.y = -1*contentInset.top;
    [self.collectionView setContentOffset:contentOffset animated:YES];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top