Updated response
I'm not 100% clear on the setup in the xib that holds the scroll view, but I'm assuming the following:
- you have one
UIView
(call in aView
) with a size on screen of 320 x 568 pixels;
- you then have a
UIScrollView
with a size on your screen of 320 x 1650 pixels.
- You have then added the scroll view to
aView
so that it is the only subview of aView
. This should mean that not all of the scroll view is visible - only the first 568 pixels (vertically).
Assuming the above I'd suggest the following:
Make sure that the top-edge of the scroll view is flush with aView
. You've probably done this, but it's worth checking. Select the scroll view, then go to the size inspector, make sure everything is zero aside from the height and width
In my app with the setup as above, I only needed the following two lines in viewDidLoad
to get the scroll view scrolling:
self.sv.frame = CGRectMake(0, 0, 320, 568);
self.sv.contentSize = CGSizeMake(320, 1650);
Note in particular the size of the frame
of the scroll view, it is NOT 1650
, it's the size that you want the scroll view to take up when it's on screen.
If I've misunderstood the setup, and this is not what you're trying to do, you should edit your question to clarify. Finally, I notice that the one property
of the scroll view that you haven't mentioned in your question is it's center
property. The bounds
, frame
, and center
are all inter-related. Try logging all three. My guess is that when you set the frame, the center
is affected, and this is why you're having to make the strange adjustment to the size of your scroll view. Assuming the scroll view covers the whole screen, the center should be (160,284)
- i.e. the midpoint of the frame
. In this situation, it should not be negative.
Initial Response
Your question implies that you've not had much luck using scroll views in conjunction with Auto-Layout:
I have turned Auto Layout off for this XIB, so that the scrollView will actually scroll.
In fact, scroll views are perfectly compatible with Auto Layout, you just have to make sure you've done a couple of things, and not done a couple of other things. I appreciate the steps below provide an indirect, rather than a direct, solution to the question you asked, but if you can get an auto-layout/scroll view setup working your current problem will disappear.
Step 1.
Select your view controller object and set it's size to Freeform (Attributes Inspector -> Simulated Metrics -> Size)
Step 2. Make your view controller as long as it needs to be to hold the entire scrollable area (1650 in your case)
Step 3. Add your scroll view to your controller's view
and make it cover the entire view area. Don't add any constraints yet, but its height should be 1650 also.
Step 4. Use Auto Layout to pin the edges of the scroll view to its superview (do NOT set any width or height attribute for the scroll view)
Step 5. It's generally a good idea to give your scroll view a content view, so add a subview view you the scroll view and make it cover the entire scroll view. This is the view which will contain the scrollable content (controls, text etc)
Step 6. Constrain the Content View in the same way that you constrained the scroll view but this time, DO set an explicit height and width (height should be 1650)
Note that at no point do you set the scroll views contentSize
, and nor do you ever give your scroll view an explicit height or width. The contentSize
value is calculated for you from the height constraint you set on the content view in Step 6.