Last active
March 6, 2018 09:53
-
-
Save sakmt/a8e908402874f43e1ddc6ff58017e572 to your computer and use it in GitHub Desktop.
UIStackView demo
This file contains 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
// スライドはこちら https://www.slideshare.net/kirimotch/uistackviewui-82938602 | |
// MARK: - ViewController.swift | |
import UIKit | |
import SnapKit | |
class ViewController: UIViewController | |
{ | |
private let colors: [UIColor] = [.red, .blue, .green, .yellow, .orange, .cyan, .magenta] | |
override func loadView() { | |
super.loadView() | |
view.backgroundColor = .white | |
let scrollView = UIScrollView().apply { this in | |
view.addSubview(this) | |
this.isPagingEnabled = true | |
this.snp.makeConstraints { make in | |
make.top.equalToSuperview().inset(100) | |
make.left.right.equalToSuperview() | |
make.height.equalTo(this.snp.width).multipliedBy(0.5) | |
} | |
} | |
let stackView = UIStackView().apply { this in | |
scrollView.addSubview(this) | |
this.snp.makeConstraints { make in | |
make.edges.height.equalToSuperview() | |
} | |
} | |
colors.forEach { color in | |
let _ = UIView().apply { this in | |
stackView.addArrangedSubview(this) | |
this.backgroundColor = color | |
this.snp.makeConstraints { make in | |
make.width.equalTo(scrollView.snp.width) | |
} | |
} | |
} | |
} | |
} | |
// MARK: - Extension.swift | |
import Foundation | |
protocol ApplyProtocol {} | |
extension ApplyProtocol { | |
func apply(_ closure: (_ this: Self) -> Void) -> Self { | |
closure(self) | |
return self | |
} | |
} | |
extension NSObject: ApplyProtocol {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment