Search code examples
iosuiscrollviewautolayoutuistoryboardcontentoffset

iOS Autolayout animation resetting contentOffset of UIScrollview


I'm trying to use auto layout with a {320,116} UIScrollView, with a content view of {960, 116}. I have made it so that the scroll view expands or collapses in height when the user scrolls up or down, and horizontal scrolling is paged (so, a total of three pages).

The problem is that when the view expands or collapses--I do this by changing top space to Top Layout Guide, the content offset value is reset to CGPointZero, so whenever the view is on page 2 or 3, its is reset to 1 against the user's will as it expands or collapses.

The leading space of UIScrollView to superview is equal to 0.


Solution

  • Two things I found out.

    1) In iOS7, if you don't specifically set the space and dimension constraints of the content view (the child view of UIScrollView) and modify a constraint constant, contentOffset is set to CGPointZero.

    2) This same problem doesn't happen in iOS8, meaning even if you don't have constraints set for the content view, contentOffset won't be reset to CGPointZero.