If you're using storyboards in Xcode 5 and developing for Auto Layout then there are a few constraints you need for it all to work (in my experience). First, lay your views out like this:
- main view
- content view (put all the stuff you want to scroll in this view)
Then do the following in this order in the storyboard editor:
- Set the content view height to 3000. Width should be 768.
- Set the scroll view height to 1024. Width should be 768.
- Set the main view height to 1024 if in freeform sizing or leave it using inferred sizing. Width should be 768.
Before you do the next steps, just double-check each view's height to be sure nothing changed. Sometimes the storyboard editor makes bizarre changes on its own.
- Set a height constraint for the content view only. It should be set at 3000.
- Pin the top, bottom, right, and left sides of the content view to the scroll view using 0 for each edge. You will have to manually change the bottom constraint from a negative number to 0. This is very important, so I'll repeat it: manually change the bottom constraint to 0.
- Pin the top, bottom, right, and left sides of the scroll view to the main view using 0 for each edge.
Now it should scroll. If you want to ensure that it stays centered when you change to a horizontal orientation, add a horizontal center constraint to the content view as well.
I have many scrolling views in my iPad app and didn't have to use the .contentSize
code once if I built my views this way in the storyboard editor.
Good luck! I know what an absolute pain and time-waster this can be.