Latest 2.0.0
Homepage https://github.com/justeat/ScrollingStackViewController
License Apache 2.0
Platforms ios 9.0
Authors , ,

ScrollingStackViewController Banner

Build Status
Version
License
Platform

ScrollingStackViewController is a convenient replacement for the UITableViewController more suitable in situations when you’re building a scrolling controller with a limited number or dynamic and rich "cells".

Motivation

UITableViewController is great for situations when we want to display an arbitrary (possibly large) number of relatively simple cells. Sometimes however you just want to partition your view controller into vertically laid out segments. The segments are probably highly heterogenous, complex, their number is well defined, but you still want to be able to show and hide them depending on the situation. You can achieve that with a UITableViewController, but it gets a litte awkward:

  • The Data Source pattern is an overkill here. You might as well just add your segments directly and hide/show them without having to go through cellForRow:at:, table view updates, etc. Juggling indexes when you want to show and hide different cells tends to be bug and crash prone and sometimes difficult to animate nicely. You probably don’t care for cell reuse in this case, so the advantages of delegation are missing here.
  • It’s difficult to partition the code well. UITableViewCell belongs in the View layer, so you either have to keep the Controller parts in your containing view controller in which case you haven’t partioned the Controller layer, or you have to pervert UITableViewCell and stick Controller code there. In either case–not a win.

The solution to the above that ScrollingStackViewController provides is to use child view controllers that can honestly keep their own Controller code while using UIStackView to deal with the layout and UIScrollView for scrolling. It’s a simple class that provides all of the scaffolding and aims to deal with all of the UIKit quirks that it likes to throw at you.

ScrollingStackViewController Demo

We invite you to check the Order Details page in the Just Eat UK app where each segment is a child view controller, making the such page a perfect use case for using ScrollingStackViewController.
The containing view controller only needs to know how to instantiate, initialise, and add the child controllers.

Usage

Inherit from ScrollingStackViewController. Instantiate and add your child view controllers. Make sure your child view controllers have constraints to self-size vertically.

class ViewController: ScrollingStackViewController {

    var viewController1: UIViewController!
    var viewController2: UIViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        viewController1 = storyboard.instantiateViewController(withIdentifier: "ChildController1") as! ChildController1
        viewController2 = storyboard.instantiateViewController(withIdentifier: "ChildController2") as! ChildController2

        add(viewController: viewController1)
        add(viewController: viewController2)            
    }
}

Insert a child view controller at position.

insert(viewController: viewController3, at: 1)

Remove a child view controller. NB: Might be easier to just add all VCs you were planning to use and then just show and hide them as needed. See below.

remove(viewController: viewController3)

Show and hide the view controllers using show(viewController:) and hide(viewController:). The transition animates.

show(viewController: viewController1)

The default is a spring animation with 0.5 duration and 1 damping. You can override the default animation closure.

animate = { animations, completion in
    UIView.animate(withDuration: 1, animations: animations, completion: completion)
}

Add spacing.

spacingColor = UIColor.lightGray
stackView.spacing = 0.5

Add border.

borderColor = UIColor.darkGray
borderWidth = 1

Programatically scroll to child view controller.

scrollTo(viewController: viewController2, action: { print("Done scrolling!") })

Override scroll animation.

scrollAnimate = { animations, completion in
    UIView.animate(withDuration: 1, animations: animations, completion: completion)
}

You still have access to the stack view, scroll view, and the background view that back the view controller if you need to do something that’s not covered quickly.

scrollView.alwaysBounceVertical = false
stackView.spacing = 1
stackViewBackgroundView.alpha = 0

Requirements

ScrollingStackViewController requires iOS 9 or higher. Swift 4, XCode 9 supported.

License

ScrollingStackViewController is available under the Apache License Version 2.0, January 2004. See the LICENSE file for more info.

  • Just Eat iOS team

Latest podspec

{
    "name": "ScrollingStackViewController",
    "version": "2.0.0",
    "summary": "A view controller that uses root views of child view controllers as views in a UIStackView.",
    "description": "This view controller is more suitable than an UITableViewController when creating a list of segments that are dynamically behaving, but are well defined and bound in number. The delegation pattern that the data source of an UITableViewController is best suited for situation when there is an unbounded number of cells, but in many cases is an overkill and becomes a burden. Also, UITableViewCells are not controllers, but sometimes it makes sense to properly partition the responsibility of the segments, not just over the view layer. Using ScrollingStackViewController you can have a bunch of view controllers, each of them encapsulating their own responsibilities.",
    "homepage": "https://github.com/justeat/ScrollingStackViewController",
    "license": {
        "type": "Apache 2.0",
        "file": "LICENSE"
    },
    "authors": {
        "Just Eat iOS team": "[email protected]",
        "Maciej Trybilo": "[email protected]",
        "Julien Regnauld": "[email protected]"
    },
    "source": {
        "git": "https://github.com/justeat/ScrollingStackViewController.git",
        "tag": "2.0.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "ScrollingStackViewController/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This