Skip to content

Instantly share code, notes, and snippets.

@Dev1an
Last active February 19, 2018 23:17
Show Gist options
  • Save Dev1an/70de3546c48295ba5c4c to your computer and use it in GitHub Desktop.
Save Dev1an/70de3546c48295ba5c4c to your computer and use it in GitHub Desktop.
An easy way to create layout constraints with some custom swift operators.
//
// 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