Last active
March 11, 2018 20:33
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍