From 553007f619dc979c4790712806785d261612fc50 Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Thu, 8 Jan 2015 14:58:12 +1300 Subject: [PATCH] Added support for UIViewController layout guides --- Snappy/Constraint.swift | 40 +++++++++++++++++++++-------- Snappy/ConstraintItem.swift | 26 ++++++++++++++++--- Snappy/ConstraintMaker.swift | 2 +- Snappy/View+Snappy.swift | 50 ++++++++++++++++++------------------ 4 files changed, 79 insertions(+), 39 deletions(-) diff --git a/Snappy/Constraint.swift b/Snappy/Constraint.swift index 34578a4..b1dfde1 100644 --- a/Snappy/Constraint.swift +++ b/Snappy/Constraint.swift @@ -50,7 +50,7 @@ public class Constraint { internal init(fromItem: ConstraintItem) { self.fromItem = fromItem - self.toItem = ConstraintItem(view: nil, attributes: ConstraintAttributes.None) + self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None) } // MARK: equalTo @@ -61,6 +61,11 @@ public class Constraint { public func equalTo(other: View) -> Constraint { return constrainTo(other, relation: .Equal) } + #if os(iOS) + public func equalTo(other: UILayoutSupport) -> Constraint { + return constrainTo(other, relation: .Equal) + } + #endif public func equalTo(other: Float) -> Constraint { return constrainTo(other, relation: .Equal) } @@ -94,6 +99,11 @@ public class Constraint { public func lessThanOrEqualTo(other: View) -> Constraint { return constrainTo(other, relation: .LessThanOrEqualTo) } + #if os(iOS) + public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint { + return constrainTo(other, relation: .LessThanOrEqualTo) + } + #endif public func lessThanOrEqualTo(other: Float) -> Constraint { return constrainTo(other, relation: .LessThanOrEqualTo) } @@ -127,6 +137,11 @@ public class Constraint { func greaterThanOrEqualTo(other: View) -> Constraint { return constrainTo(other, relation: .GreaterThanOrEqualTo) } + #if os(iOS) + public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint { + return constrainTo(other, relation: .GreaterThanOrEqualTo) + } + #endif public func greaterThanOrEqualTo(other: Float) -> Constraint { return constrainTo(other, relation: .GreaterThanOrEqualTo) } @@ -326,9 +341,9 @@ public class Constraint { public func uninstall() { if let view = self.installedOnView { #if os(iOS) - var installedConstraints = view.constraints() - #else - var installedConstraints = view.constraints + var installedConstraints = view.constraints() + #else + var installedConstraints = view.constraints #endif var constraintsToRemove: Array = [] for installedConstraint in installedConstraints { @@ -381,27 +396,32 @@ public class Constraint { return self } private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint { - return constrainTo(ConstraintItem(view: other, attributes: ConstraintAttributes.None), relation: relation) + return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) } + #if os(iOS) + private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint { + return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) + } + #endif private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint { self.constant = other - return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) } private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint { self.constant = other - return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) } private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint { self.constant = other - return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) } private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint { self.constant = other - return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) } private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint { self.constant = other - return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) } private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? { diff --git a/Snappy/ConstraintItem.swift b/Snappy/ConstraintItem.swift index 557af6d..e0d392c 100644 --- a/Snappy/ConstraintItem.swift +++ b/Snappy/ConstraintItem.swift @@ -32,11 +32,31 @@ import AppKit */ public class ConstraintItem { - internal init(view: View?, attributes: ConstraintAttributes) { - self.view = view + internal init(object: AnyObject?, attributes: ConstraintAttributes) { + self.object = object self.attributes = attributes } - internal weak var view: View? + internal weak var object: AnyObject? internal var attributes: ConstraintAttributes + + internal var view: View? { + get { + if let view = self.object as? View { + return view + } + return nil + } + } + + #if os(iOS) + internal var layoutSupport: UILayoutSupport? { + get { + if let layoutSupport = self.object as? UILayoutSupport { + return layoutSupport + } + return nil + } + } + #endif } \ No newline at end of file diff --git a/Snappy/ConstraintMaker.swift b/Snappy/ConstraintMaker.swift index f9ae7ad..640db23 100644 --- a/Snappy/ConstraintMaker.swift +++ b/Snappy/ConstraintMaker.swift @@ -55,7 +55,7 @@ public class ConstraintMaker { internal var constraints = Array() internal func addConstraint(attributes: ConstraintAttributes) -> Constraint { - let item = ConstraintItem(view: self.view, attributes: attributes) + let item = ConstraintItem(object: self.view, attributes: attributes) let constraint = Constraint(fromItem: item) self.constraints.append(constraint) return constraint diff --git a/Snappy/View+Snappy.swift b/Snappy/View+Snappy.swift index 2705549..ff174c2 100644 --- a/Snappy/View+Snappy.swift +++ b/Snappy/View+Snappy.swift @@ -30,35 +30,35 @@ public typealias View = NSView #endif public extension View { - public var snp_left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) } - public var snp_top: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Top) } - public var snp_right: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Right) } - public var snp_bottom: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Bottom) } - public var snp_leading: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Leading) } - public var snp_trailing: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Trailing) } - public var snp_width: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Width) } - public var snp_height: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Height) } - public var snp_centerX: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterX) } - public var snp_centerY: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterY) } - public var snp_baseline: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Baseline) } + public var snp_left: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Left) } + public var snp_top: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Top) } + public var snp_right: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Right) } + public var snp_bottom: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Bottom) } + public var snp_leading: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Leading) } + public var snp_trailing: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Trailing) } + public var snp_width: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Width) } + public var snp_height: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Height) } + public var snp_centerX: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterX) } + public var snp_centerY: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterY) } + public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) } - public var snp_firstBaseline: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.FirstBaseline) } + public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) } - public var snp_leftMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.LeftMargin) } - public var snp_rightMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.RightMargin) } - public var snp_topMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.TopMargin) } - public var snp_bottomMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.BottomMargin) } - public var snp_leadingMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.LeadingMargin) } - public var snp_trailingMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.TrailingMargin) } - public var snp_centerXWithinMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterXWithinMargins) } - public var snp_centerYWithinMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterYWithinMargins) } + public var snp_leftMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeftMargin) } + public var snp_rightMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.RightMargin) } + public var snp_topMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TopMargin) } + public var snp_bottomMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.BottomMargin) } + public var snp_leadingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeadingMargin) } + public var snp_trailingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TrailingMargin) } + public var snp_centerXWithinMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterXWithinMargins) } + public var snp_centerYWithinMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) } - public var snp_edges: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Edges) } - public var snp_size: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Size) } - public var snp_center: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Center) } + public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) } + public var snp_size: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Size) } + public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) } - public var snp_margins: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Margins) } - public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterWithinMargins) } + public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) } + public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) } public func snp_makeConstraints(block: (maker: ConstraintMaker) -> ()) { ConstraintMaker.makeConstraints(self, block: block)