In xcode 5 using storyboards how would one make a fully operational vertical scrolling scrollview, with AutoLayout ON?
Considering the subviews have hierarchy:
1.UIView
2.UIScrollView
3.UIView (lets call this UIDetailView to make things easier)
Please be specific from code to constraints to wether any of the views HAS to be smaller etc.
UIScrollView with Autolayout within Storyboards Just Works
I've seen a number of people recommending the 'Container View' approach, AKA brute force, to solving the problem that they don't understand. It is non-optimal since you now have lost a big advantage of the scrollview by making it think the content is the entire scrollview rather than the subviews immediately attached to the scrollview.
Here is how I did it in the example that follows
When placing a
UIScrollView
into aUIView
in a Storyboard just pin the edges to the 4 sides of theUIScrollView
to theUIView
. Now add your content to theUIScrollView
making sure that you provide a minimum of two constraints for each dimension. The great thing about Autolayout is that it figures out how big the contentSize of the scrollview, or UILabels for that matter, needs to be based upon the size of the content inside it. AKA intrinsicContentSize. So if you are given a warning 'Ambiguous content size for scrollView' you know that you have not given the content enough constraints. For example, you might have given Top, Bottom, Left, Right spacing distance between views but the subview you're constraining needs a height too since an infinite vertical plane like thisUIScrollView
could assume your view was from zero to infinitely high.To put it another way the Apple guide on Autolayout by Example makes a simple 3 point plan for success:
That top TextView with 'Min melding til' is also growing as you type more lines into it and the whole ScrollView grows to contain it. While I override the
UITextView
class to return a modified height constraint, the ScrollView itself works correctly without coding.One last thing, lots of posts related to Autolayout try the magical fix-all incantation
translatesAutoresizingMaskIntoConstraints = NO
. This is only necessary if the view is created programmatically.This blog post details how to use a UIScrollView with Autolayout ON, using a pure autolayout approach. Note though that all constraints in the blog post are defined through the Storyboard.
The approach in the post assumes the following hierarchy:
I guess the Container View will be your UIDetailView, and the Content View will be any UIView inside your UIDetailView.
https://happyteamlabs.com/blog/ios-how-to-use-uiscrollview-with-auto-layout-pure-auto-layout/
The documentation clearly states how to do this: A UIScrollView in auto-layout will always resize itself to fit the content (UIDetailView).
So you have to set up your views like this:
I had a similar problem and i found relative simple solution similar to DJ S's from within Interface Builder using pure Autolayout without any code.
For proof-of-concept at first remove any constraint in View Controller to if see this works.
This is sample layout:
View (main view of my UIViewController)
Scroll View (UIScrollView)
Container View (UIView)
A. Scroll View width/height should be smaller that Container View width/height
B. Container View should have some determinated width/height (may be explicit width/height )
C. Do Control-drag Container View to Scroll View and add only:
D. Check out those two constraints and set "constant" value for both to 0
E. Run app and
Because of the new iPhone 6 and 6+ screen sizes, I had to make a few tweaks to DJ S's solution.
The goal
Position a
UITextView
inside aUIScrollView
, and also have15
pt spaces from the left/right screen edges.Views
Solution
For the spaces, I added 15 pt horizontal trailing/leading spaces from
UIScrollView
->Main View
. To make theUITextView
's width relative to the screen width, I added anEqual Widths
constraint fromUITextView
->Main View
and set the value to-30
(2 * the 15 pt horizontal space). Now, theUITextView
's width will dynamically adjust for any screen size.The
UIScrollView
should haveScrolling Enabled
. TheUITextView
should not.