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

99 lines
2.8 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
let blackView: UIView = {
let view = UIView()
view.backgroundColor = .blackColor()
return view
}()
2016-03-01 19:16:44 +08:00
let redView: UIView = {
let view = UIView()
view.backgroundColor = .redColor()
return view
}()
2016-03-01 19:20:30 +08:00
let yellowView: UIView = {
let view = UIView()
view.backgroundColor = .yellowColor()
return view
}()
let blueView: UIView = {
let view = UIView()
view.backgroundColor = .blueColor()
return view
}()
2016-03-01 19:23:33 +08:00
let greenView: UIView = {
let view = UIView()
view.backgroundColor = .greenColor()
return view
}()
2016-03-01 19:13:24 +08:00
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
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-03-01 19:13:24 +08:00
view.setNeedsUpdateConstraints()
}
override func updateViewConstraints() {
if (!didSetupConstraints) {
blackView.snp_makeConstraints(closure: { (make) -> Void in
make.center.equalTo(view)
make.size.equalTo(CGSizeMake(100.0, 100.0))
})
2016-03-01 19:16:44 +08:00
redView.snp_makeConstraints(closure: { (make) -> Void in
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))
})
yellowView.snp_makeConstraints(closure: { (make) -> Void in
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))
})
blueView.snp_makeConstraints(closure: { (make) -> Void 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-03-01 19:23:33 +08:00
greenView.snp_makeConstraints(closure: { (make) -> Void in
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-03-01 19:13:24 +08:00
didSetupConstraints = true
}
super.updateViewConstraints()
}
}