SnapKit/Example-iOS/demos/SimpleLayoutViewController....

99 lines
2.6 KiB
Swift
Raw Normal View History

2016-03-01 19:13:24 +08:00
//
// SimpleLayoutViewController.swift
// SnapKit
//
// Created by Spiros Gerokostas on 01/03/16.
// Copyright © 2016 SnapKit Team. All rights reserved.
//
import UIKit
class SimpleLayoutViewController: UIViewController {
var didSetupConstraints = false
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
let blackView: UIView = {
let view = UIView()
view.backgroundColor = .blackColor()
return view
}()
2016-05-12 16:55:55 +08:00
2016-03-01 19:16:44 +08:00
let redView: UIView = {
let view = UIView()
view.backgroundColor = .redColor()
return view
}()
2016-05-12 16:55:55 +08:00
2016-03-01 19:20:30 +08:00
let yellowView: UIView = {
let view = UIView()
view.backgroundColor = .yellowColor()
return view
}()
2016-05-12 16:55:55 +08:00
2016-03-01 19:20:30 +08:00
let blueView: UIView = {
let view = UIView()
view.backgroundColor = .blueColor()
return view
}()
2016-05-12 16:55:55 +08:00
2016-03-01 19:23:33 +08:00
let greenView: UIView = {
let view = UIView()
view.backgroundColor = .greenColor()
return view
}()
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
override func viewDidLoad() {
super.viewDidLoad()
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
view.backgroundColor = UIColor.whiteColor()
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
view.addSubview(blackView)
2016-03-01 19:16:44 +08:00
view.addSubview(redView)
2016-03-01 19:20:30 +08:00
view.addSubview(yellowView)
view.addSubview(blueView)
2016-03-01 19:23:33 +08:00
view.addSubview(greenView)
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
view.setNeedsUpdateConstraints()
}
override func updateViewConstraints() {
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
if (!didSetupConstraints) {
2016-05-12 16:55:55 +08:00
blackView.snp_makeConstraints { make in
2016-03-01 19:13:24 +08:00
make.center.equalTo(view)
make.size.equalTo(CGSizeMake(100.0, 100.0))
2016-05-12 16:55:55 +08:00
}
redView.snp_makeConstraints { make in
2016-03-01 19:16:44 +08:00
make.top.equalTo(blackView.snp_bottom).offset(20.0)
2016-03-01 19:20:30 +08:00
make.left.equalTo(20.0)
make.size.equalTo(CGSizeMake(100.0, 100.0))
2016-05-12 16:55:55 +08:00
}
yellowView.snp_makeConstraints { make in
2016-03-01 19:20:30 +08:00
make.top.equalTo(blackView.snp_bottom).offset(20.0)
make.left.equalTo(blackView.snp_right).offset(20.0)
make.size.equalTo(CGSizeMake(100.0, 100.0))
2016-05-12 16:55:55 +08:00
}
blueView.snp_makeConstraints { make in
2016-03-01 19:23:33 +08:00
make.bottom.equalTo(blackView.snp_top).offset(-20.0)
2016-03-01 19:20:30 +08:00
make.left.equalTo(blackView.snp_right).offset(20.0)
2016-03-01 19:16:44 +08:00
make.size.equalTo(CGSizeMake(100.0, 100.0))
2016-05-12 16:55:55 +08:00
}
greenView.snp_makeConstraints { make in
2016-03-01 19:23:33 +08:00
make.bottom.equalTo(blackView.snp_top).offset(-20.0)
make.right.equalTo(blackView.snp_left).offset(-20.0)
make.size.equalTo(CGSizeMake(100.0, 100.0))
2016-05-12 16:55:55 +08:00
}
2016-03-01 19:13:24 +08:00
didSetupConstraints = true
}
2016-05-12 16:55:55 +08:00
2016-03-01 19:13:24 +08:00
super.updateViewConstraints()
}
}