Scrolling a UIScrollView changes its subviews frames

autolayout, cocoa-touch, ios, uiscrollview

Solution

Frames and `Auto Layout` doesn't go together, If you are using `Auto Layout` you'll need to rearrange the constrains, not the frames. While using `Auto Layout` Changing the frames will cause some weird effects and will eventually revert back to the constraints you've created in the original `UIView`.

A few solutions:

- You can create an outlet to each constrain just like you would to a view and change its constant when needed.

- If you are using xib you can disable `Auto Layout` in that specific xib.

Problem

My app uses storyboards and autolayout. In a view controller I created a `UIScrollView` and added three subviews. At run time I need to change the sizes of the subviews and rearrange them. Initially, I rearrange them in `viewDidLayoutSubviews`, and it seems to work until I scroll the scroll view. Then the subview frames are changed back to the sizes that are set in IB. (I'm setting the `UIScrollView`'s content size when rearranging the views and removing all `UIViewConstraints`.)

Original source