Skip to content

Instantly share code, notes, and snippets.

@pixeldock
Last active March 11, 2018 20:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixeldock/3172b70b6b7a96f30f0300ac464f7e18 to your computer and use it in GitHub Desktop.
Save pixeldock/3172b70b6b7a96f30f0300ac464f7e18 to your computer and use it in GitHub Desktop.
Generic UIViewController with horizontal UIScrollView that was created programatically with Auto Layout using SnapKit
//
// ViewController.swift
// HorizontalScrollView
//
// Created by Jörn Schoppe on 13.04.16.
// Copyright © 2016 pixeldock. All rights reserved.
//
import UIKit
import SnapKit
class ViewController: UIViewController {
let scrollView = UIScrollView()
let subViews = [UIView(), UIView(), UIView(), UIView()]
let colors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.redColor(), UIColor.orangeColor()]
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.snp_makeConstraints { (make) in
make.edges.equalTo(view)
}
subViews.enumerate().forEach { index, subview in
subview.backgroundColor = colors[index]
scrollView.addSubview(subview)
subview.snp_makeConstraints(closure: { (make) in
make.top.equalTo(0)
make.size.equalTo(scrollView)
switch index {
case 0:
make.left.equalTo(0)
case subViews.count - 1:
make.left.equalTo(subViews[index - 1].snp_right)
make.right.equalTo(0)
default:
make.left.equalTo(subViews[index - 1].snp_right)
}
})
}
}
}
@andrewrjones
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment