Vitamin C

iOS UIScrollView AutoLayout


When using Autolayout with a UIScrollView, it does NOT resize properly depending on the size of its contents or subviews.

In order to get a UIScrollView to automatically scroll when its contents become too large to fit in the visible area, we need to add a ContentView and some constraints that allow the UIScrollView to determine the size of its contents AND its width and height in its parent view.

import Foundation
import UIKit

class ScrollableController : UIViewController {
    private var scrollView: UIScrollView!
    private var contentView: UIView!
    override func viewDidLoad() {
        //Add child views
    func initControls() {
        self.scrollView = UIScrollView()
        self.contentView = UIView()
    func setTheme() {
        self.scrollView.backgroundColor =
        self.contentView.backgroundColor =
    func layoutScrollView() {
        let views: NSDictionary = ["scrollView": self.scrollView]
        var constraints = Array<String>()
        //Constrain the scrollView to our controller's self.view.
        for constraint in constraints {
            self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: constraint, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views as! [String : AnyObject]))
        self.scrollView.translatesAutoresizingMaskIntoConstraints = false
    func layoutContentView() {
        let views: NSDictionary = ["contentView": self.contentView, "view": self.view]
        var constraints = Array<String>()
        //Constrain the contentView to the scrollView.
        for constraint in constraints {
            self.scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: constraint, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views as! [String : AnyObject]))
        //Disable Horizontal Scrolling by making the contentView EqualWidth with our controller's self.view (ScrollView's parentView).
        self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[contentView(==view)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views as! [String : AnyObject]))
        self.contentView.translatesAutoresizingMaskIntoConstraints = false
    func addChildViews() {
        let greenView = UIView()
        let whiteView = UIView()
        greenView.backgroundColor =
        whiteView.backgroundColor =
        //Layout -- Child views are added to the 'ContentView'
        let views: NSDictionary = ["greenView": greenView, "whiteView": whiteView];
        var constraints = Array<String>()
        //Constrain the greenView to the contentView with a height of 400 and 15 spacing all around.
        //Constrain the whiteView below the greenView with 15 spacing all around and a height of 500.
        for constraint in constraints {
            self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: constraint, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views as! [String : AnyObject]))
        greenView.translatesAutoresizingMaskIntoConstraints = false
        whiteView.translatesAutoresizingMaskIntoConstraints = false

Now we can see that the greenView (400 height) + the whiteView (500 height) is larger than our screen. This will cause the ScrollView's contentSize to grow to fit BOTH views, allowing it to scroll vertically.

We disabled horizontal scrolling using the EqualWidth constraint on the contentView and self.view

enter image description here

UIScrollView dynamic content size through Storyboard

While using scrollviews in storyboard it's better to calculate content size according to number of views present in scrollview rather than giving content size programatically with static value.

Here are the steps to get content size dynamically.

Step 1 :

Add Scrollview to view in storyboard and add leading, trailing, top and bottom constraints (All values are zero).

Step 2 :

Don't add directly views which you need on directly scrollview, First add one view to scrollview (that will be our content view for all UI elements). Add below constraints to this view.

  1. Leading, trailing, top and bottom constraints (All values are zero).

  2. Add equal height, equal width to Main view (i.e. which contains scrollview). For equal height set priority to low. (This is the important step for setting content size).

  3. Height of this content view will be according to the number of views added to the view. let say if you added last view is one label and his Y position is 420 and height is 20 then your content view will be 440.

Step 3 : Add constraints to all of views which you added within content view as per your requirement.

enter image description here

enter image description here

Got any iOS Question?