Giter Site home page Giter Site logo

surgiollc / scrollingstackviewcontroller Goto Github PK

View Code? Open in Web Editor NEW

This project forked from justeat/scrollingstackviewcontroller

0.0 3.0 0.0 1.12 MB

A view controller that uses root views of child view controllers as views in a UIStackView.

License: Apache License 2.0

Ruby 9.23% Swift 90.77%

scrollingstackviewcontroller's Introduction

ScrollingStackViewController Banner

ScrollingStackViewController

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

scrollingstackviewcontroller's People

Contributors

albertodebortoli avatar federicocappelli-je avatar maciejtrybilo avatar

Watchers

Jeff Claeson avatar James Cloos avatar Chandler De Angelis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.