Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@andrewrjones andrewrjones commented Mar 11, 2018

👍

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