Last active
August 29, 2015 14:25
-
-
Save Pretz/bd3310a829b1c102702e to your computer and use it in GitHub Desktop.
Strutter
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
//: Playground - noun: a place where people can play | |
import UIKit | |
infix operator |=| { associativity left } | |
infix operator |=>| { associativity left } | |
infix operator |=<| { associativity left } | |
private func _install(constraint: NSLayoutConstraint) { | |
// Only disable AutoresizineMask on left item, secondItem may need it enabled | |
if let v1 = constraint.firstItem as? UIView where v1.superview != nil { | |
v1.translatesAutoresizingMaskIntoConstraints = false | |
} | |
constraint.active = true | |
} | |
func |=|<T: NSLayoutAnchor>(leftAnchor: T, pair: (NSLayoutAnchor, CGFloat)) -> T { | |
_install(leftAnchor.constraintEqualToAnchor(pair.0, constant: pair.1)) | |
return leftAnchor | |
} | |
func |=|<T: NSLayoutAnchor>(leftAnchor: T, rightAnchor: NSLayoutAnchor) -> T { | |
leftAnchor |=| (rightAnchor, 0) | |
return leftAnchor | |
} | |
func |=>|<T: NSLayoutAnchor>(leftAnchor: T, rightAnchor: NSLayoutAnchor) -> T { | |
leftAnchor |=>| (rightAnchor, 0) | |
return leftAnchor | |
} | |
func |=>|<T: NSLayoutAnchor>(leftAnchor: T, pair: (NSLayoutAnchor, CGFloat)) -> T { | |
_install(leftAnchor.constraintGreaterThanOrEqualToAnchor(pair.0, constant: pair.1)) | |
return leftAnchor | |
} | |
func |=<|<T: NSLayoutAnchor>(leftAnchor: T, rightAnchor: NSLayoutAnchor) -> T { | |
leftAnchor |=<| (rightAnchor, 0) | |
return leftAnchor | |
} | |
func |=<|<T: NSLayoutAnchor>(leftAnchor: T, params: (rightAnchor: NSLayoutAnchor, constant: CGFloat)) -> T { | |
_install(leftAnchor.constraintLessThanOrEqualToAnchor(params.0, constant: params.1)) | |
return leftAnchor | |
} | |
func |=|<T: NSLayoutDimension>(leftAnchor: T, constant: CGFloat) -> T { | |
_install(leftAnchor.constraintEqualToConstant(constant)) | |
return leftAnchor | |
} | |
func |=|<T: NSLayoutDimension>(leftAnchor: T, rest: (NSLayoutDimension, CGFloat, CGFloat)) -> T { | |
_install(leftAnchor.constraintEqualToAnchor(rest.0, multiplier: rest.1, constant: rest.2)) | |
return leftAnchor | |
} | |
func |=|<T: NSLayoutDimension>(leftAnchor: T, rest: (NSLayoutDimension, CGFloat)) -> T { | |
leftAnchor |=| (rest.0, rest.1, 1) | |
return leftAnchor | |
} | |
let v = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 140)) | |
let subv = UIView() | |
v.backgroundColor = UIColor.whiteColor() | |
subv.backgroundColor = UIColor.blueColor() | |
v.addSubview(subv) | |
subv.widthAnchor |=| 20 | |
subv.heightAnchor |=| 30 | |
subv.centerXAnchor |=| v.centerXAnchor | |
subv.topAnchor |=| (v.topAnchor, 10) | |
let subv2 = UIView() | |
subv2.backgroundColor = UIColor.greenColor() | |
v.addSubview(subv2) | |
let subv3 = UIView() | |
subv3.backgroundColor = UIColor.orangeColor() | |
v.addSubview(subv3) | |
subv2.widthAnchor |=| subv3.widthAnchor |=| subv.widthAnchor | |
subv2.heightAnchor |=| subv3.heightAnchor |=| subv.heightAnchor | |
subv2.leftAnchor |=| subv3.leftAnchor |=| subv.leftAnchor | |
subv2.topAnchor |=| (subv.bottomAnchor, 10) | |
subv3.topAnchor |=| (subv2.bottomAnchor, 10) | |
v.layoutIfNeeded() | |
v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment