Last active
February 19, 2018 23:17
-
-
Save Dev1an/70de3546c48295ba5c4c to your computer and use it in GitHub Desktop.
An easy way to create layout constraints with some custom swift operators.
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
// | |
// ConstraintOperators.swift | |
// | |
// Created by Damiaan Dufaux on 10/12/15. | |
// Copyright © 2015 Damiaan Dufaux. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
infix operator -|- {} | |
infix operator |- {} | |
infix operator ÷ {} | |
infix operator |÷ {} | |
infix operator ÷| {} | |
func -|- (left: UIView, right: UIView) -> (multiplier: CGFloat, constant: CGFloat) -> NSLayoutConstraint { | |
return { multiplier, constant in | |
let c = NSLayoutConstraint(item: left, attribute: .Trailing, relatedBy: .Equal, toItem: right, attribute: .Leading, multiplier: multiplier, constant: constant) | |
c.priority = 50 | |
return c | |
} | |
} | |
func |- (left: UIView, right: UIView) -> (multiplier: CGFloat, constant: CGFloat) -> NSLayoutConstraint { | |
return { multiplier, constant in | |
let c = NSLayoutConstraint(item: left, attribute: .Leading, relatedBy: .Equal, toItem: right, attribute: .Leading, multiplier: multiplier, constant: constant) | |
c.priority = 50 | |
return c | |
} | |
} | |
func ÷ (left: UIView, right: UIView) -> (multiplier: CGFloat, constant: CGFloat) -> NSLayoutConstraint { | |
return { multiplier, constant in | |
let c = NSLayoutConstraint(item: left, attribute: .Bottom, relatedBy: .Equal, toItem: right, attribute: .Top, multiplier: multiplier, constant: constant) | |
c.priority = 50 | |
return c | |
} | |
} | |
func |÷ (left: UIView, right: UIView) -> (multiplier: CGFloat, constant: CGFloat) -> NSLayoutConstraint { | |
return { multiplier, constant in | |
let c = NSLayoutConstraint(item: left, attribute: .Top, relatedBy: .Equal, toItem: right, attribute: .Top, multiplier: multiplier, constant: constant) | |
c.priority = 50 | |
return c | |
} | |
} | |
func ÷| (left: UIView, right: UIView) -> (multiplier: CGFloat, constant: CGFloat) -> NSLayoutConstraint { | |
return { multiplier, constant in | |
let c = NSLayoutConstraint(item: left, attribute: .Bottom, relatedBy: .Equal, toItem: right, attribute: .Bottom, multiplier: multiplier, constant: constant) | |
c.priority = 50 | |
return c | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment