Skip to content

Instantly share code, notes, and snippets.

@benjiwheeler
Created July 7, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjiwheeler/6f0ebbac3c847b87a61d to your computer and use it in GitHub Desktop.
Save benjiwheeler/6f0ebbac3c847b87a61d to your computer and use it in GitHub Desktop.
NSLayoutConstraint+Init.swift
//
// NSLayoutConstraint+Init.swift
// Created by Benjamin Wheeler starting 6/2013
//
//
import UIKit
// improves the clarity and flexibility of NSLayoutConstraint
extension NSLayoutConstraint {
convenience init(fromItem: UIView, fromAttribute: NSLayoutAttribute, relatedBy: NSLayoutRelation=NSLayoutRelation.Equal, toItem toItemParam: UIView?=nil, toAttribute toAttributeParam: NSLayoutAttribute?=nil, multiplier: CGFloat=1.0, constant: CGFloat=0.0) {
var toItemResolved: UIView? = toItemParam
if toItemResolved == nil {
toItemResolved = fromItem
}
var toAttributeResolved: NSLayoutAttribute? = toAttributeParam
if toAttributeResolved == nil {
toAttributeResolved = fromAttribute
}
self.init(item: fromItem, attribute: fromAttribute, relatedBy: relatedBy, toItem: toItemResolved!, attribute: toAttributeResolved!, multiplier: multiplier, constant: constant)
}
class func add(commonAncestor: UIView, fromItem: UIView, fromAttribute: NSLayoutAttribute, relatedBy: NSLayoutRelation=NSLayoutRelation.Equal, toItem toItemParam: UIView?=nil, toAttribute toAttributeParam: NSLayoutAttribute?=nil, multiplier: CGFloat=1.0, constant: CGFloat=0.0) {
var toItemResolved: UIView? = toItemParam
if toItemResolved == nil {
toItemResolved = fromItem
}
var toAttributeResolved: NSLayoutAttribute? = toAttributeParam
if toAttributeResolved == nil {
toAttributeResolved = fromAttribute
}
let constraintToAdd = NSLayoutConstraint(fromItem: fromItem, fromAttribute: fromAttribute, relatedBy: relatedBy, toItem: toItemResolved!, toAttribute: toAttributeResolved!, multiplier: multiplier, constant: constant)
commonAncestor.addConstraint(constraintToAdd)
}
class func setEdges(commonAncestor: UIView, fromItem: UIView, toItem: UIView) {
commonAncestor.addConstraint(NSLayoutConstraint(fromItem: fromItem, fromAttribute: .Top, toItem: toItem))
commonAncestor.addConstraint(NSLayoutConstraint(fromItem: fromItem, fromAttribute: .Bottom, toItem: toItem))
commonAncestor.addConstraint(NSLayoutConstraint(fromItem: fromItem, fromAttribute: .Left, toItem: toItem))
commonAncestor.addConstraint(NSLayoutConstraint(fromItem: fromItem, fromAttribute: .Right, toItem: toItem))
}
class func setWidth(item: UIView, width: CGFloat) {
item.addConstraint(NSLayoutConstraint(fromItem: item, fromAttribute: NSLayoutAttribute.Width, constant: width))
}
class func setHeight(item: UIView, height: CGFloat) {
item.addConstraint(NSLayoutConstraint(fromItem: item, fromAttribute: NSLayoutAttribute.Height, constant: height))
}
}
// lets constraints know about their owners, so they can add and remove themselves at will
class Straint {
var constraint: NSLayoutConstraint
var commonAncestor: UIView?
init(commonAncestor: UIView, fromItem: UIView, fromAttribute: NSLayoutAttribute, relatedBy: NSLayoutRelation=NSLayoutRelation.Equal, toItem: UIView?=nil, toAttribute: NSLayoutAttribute?=nil, multiplier: CGFloat=1.0, constant: CGFloat=0.0) {
self.constraint = NSLayoutConstraint(fromItem: fromItem, fromAttribute: fromAttribute, relatedBy: relatedBy, toItem: toItem, toAttribute: toAttribute, multiplier: multiplier, constant: constant)
self.commonAncestor = commonAncestor
}
class func add(commonAncestor: UIView, fromItem: UIView, fromAttribute: NSLayoutAttribute, relatedBy: NSLayoutRelation=NSLayoutRelation.Equal, toItem: UIView?=nil, toAttribute: NSLayoutAttribute?=nil, multiplier: CGFloat=1.0, constant: CGFloat=0.0) {
let newStraint = Straint(commonAncestor: commonAncestor, fromItem: fromItem, fromAttribute: fromAttribute, relatedBy: relatedBy, toItem: toItem, toAttribute: toAttribute, multiplier: multiplier, constant: constant)
newStraint.add()
}
func add() -> Bool {
if self.commonAncestor != nil {
self.commonAncestor!.addConstraint(self.constraint)
return true
}
return false
}
func remove() -> Bool {
if self.commonAncestor != nil {
self.commonAncestor!.removeConstraint(self.constraint)
return true
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment