I put a UIScrollView
in my nib's view
, and linked it to a an IBOutlet
property.
Now, when I do this in my viewDidLoad
method, it seems to have no effect on the contentSize
:
self.sv.backgroundColor = [UIColor yellowColor]; // this works
CGSize size = CGSizeMake(1000.0, 1000.0);
[self.sv setContentSize:size]; // this does not
It behaves as if the contentSize
was the same as the frame. What's going on?
This started working when I turned off AutoLayout. Why?
I had the same problem. Auto Layout for
UIScrollView
is messed up.Work around: Put everything in the
UIScrollView
into anotherUIView
, and put thatUIView
as the only child of theUIScrollView
. Then you can use Auto Layout.If things near the end is messed up (the end of whichever direction your
UIScrollView
scrolls), change the constraint at the end to have the lowest possible priority.I tried viewWillLayoutSubviews to update scrollView's contentSize, it worked for me.
Apple Doc
Called to notify the view controller that its view has just laid out its subviews.
Discussion
When the bounds change for a view controller’s view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view’s subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.
I could never get auto layout based on constraints to work. Since my view was already a subclass UIScrollView I solved it by overriding setContentView: and ignoring auto layouts zero height setContentSize: message.
I got Autolayout to work for paginated scroll views whose pages occupy the full-width of the screen. The pages automatically resize according to the scroll view's size. I haven't tested this for lesser-width scroll views but do comment away if it works--I beleieve it should. Targeted for iOS 9, wrote code in Swift 2, used a mix of IB's and custom code in
awakeFromNib
.Steps:
UIView
(I called minecontentView
) whose top, trailing, bottom, and leading edges to the scroll view are all zero; the height is equal to the scroll view's; but the width is the scroll view's width times the number of pages. If you're doing this visually, you will see your content view extend beyond your scroll view in Inteface Builder.contentView
, add Autolayout rules to put them side-by-side each other, but most importantly, give them each a constraint so that their widths are equal to the scroll view's, not the content view's.Sample code below.
embedChildViewController
is just my convenience method for adding child VCs--do look atsetupLayoutRulesForPages
. I have exactly two pages so the function is too simple, but you can expand it to your needs.In my view controller:
My custom view:
If you are using
AutoLayout
a really easy way to set thecontentSize
of aUIScrollView
is just to add something like this:A SUPER easy way to use AutoLayout with UIScrollViews inside Interface Builder:
Step 1: Create a
UIScrollView
Step 2: Create a
UIView
that is a child of your scroll view like so:(We'll call this one
contentView
).Step 3: In the size inspector, give this view a height and width (say, 320x700).
Step 4 (using AutoLayout): Create unambiguous constraints from your
contentView
to its superview (theUIScrollView
): connect the 4 edges (top, leading, trailing, bottom), then give it a defined width and height that you want it to scroll too.For example: If your scroll view spans the entire screen, you could give your content view a width of [device width] and a height of 600; it will then set the content size of the
UIScrollView
to match.OR:
Step 4 (not using AutoLayout): Connect both of these new controls to your view controller using IB (ctrl+drag from each control to your view controller's .h @implementation). Let's assume each is called
scrollView
andcontentView
, respectively. It should look like this:Step 5 (not using AutoLayout): In the view controller's .h file add (actually, override) the following method: