Added support for UIViewController layout guides

This commit is contained in:
Robert Payne 2015-01-08 14:58:12 +13:00
parent ef24bbeb94
commit 553007f619
4 changed files with 79 additions and 39 deletions

View File

@ -50,7 +50,7 @@ public class Constraint {
internal init(fromItem: ConstraintItem) { internal init(fromItem: ConstraintItem) {
self.fromItem = fromItem self.fromItem = fromItem
self.toItem = ConstraintItem(view: nil, attributes: ConstraintAttributes.None) self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
} }
// MARK: equalTo // MARK: equalTo
@ -61,6 +61,11 @@ public class Constraint {
public func equalTo(other: View) -> Constraint { public func equalTo(other: View) -> Constraint {
return constrainTo(other, relation: .Equal) 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 { public func equalTo(other: Float) -> Constraint {
return constrainTo(other, relation: .Equal) return constrainTo(other, relation: .Equal)
} }
@ -94,6 +99,11 @@ public class Constraint {
public func lessThanOrEqualTo(other: View) -> Constraint { public func lessThanOrEqualTo(other: View) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) 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 { public func lessThanOrEqualTo(other: Float) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return constrainTo(other, relation: .LessThanOrEqualTo)
} }
@ -127,6 +137,11 @@ public class Constraint {
func greaterThanOrEqualTo(other: View) -> Constraint { func greaterThanOrEqualTo(other: View) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) 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 { public func greaterThanOrEqualTo(other: Float) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
@ -326,9 +341,9 @@ public class Constraint {
public func uninstall() { public func uninstall() {
if let view = self.installedOnView { if let view = self.installedOnView {
#if os(iOS) #if os(iOS)
var installedConstraints = view.constraints() var installedConstraints = view.constraints()
#else #else
var installedConstraints = view.constraints var installedConstraints = view.constraints
#endif #endif
var constraintsToRemove: Array<LayoutConstraint> = [] var constraintsToRemove: Array<LayoutConstraint> = []
for installedConstraint in installedConstraints { for installedConstraint in installedConstraints {
@ -381,27 +396,32 @@ public class Constraint {
return self return self
} }
private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint { 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 { private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
self.constant = other 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 { private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
self.constant = other 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 { private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
self.constant = other 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 { private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
self.constant = other 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 { private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
self.constant = other 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? { private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {

View File

@ -32,11 +32,31 @@ import AppKit
*/ */
public class ConstraintItem { public class ConstraintItem {
internal init(view: View?, attributes: ConstraintAttributes) { internal init(object: AnyObject?, attributes: ConstraintAttributes) {
self.view = view self.object = object
self.attributes = attributes self.attributes = attributes
} }
internal weak var view: View? internal weak var object: AnyObject?
internal var attributes: ConstraintAttributes 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
} }

View File

@ -55,7 +55,7 @@ public class ConstraintMaker {
internal var constraints = Array<Constraint>() internal var constraints = Array<Constraint>()
internal func addConstraint(attributes: ConstraintAttributes) -> Constraint { 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) let constraint = Constraint(fromItem: item)
self.constraints.append(constraint) self.constraints.append(constraint)
return constraint return constraint

View File

@ -30,35 +30,35 @@ public typealias View = NSView
#endif #endif
public extension View { public extension View {
public var snp_left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) } public var snp_left: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Left) }
public var snp_top: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Top) } public var snp_top: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Top) }
public var snp_right: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Right) } public var snp_right: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Right) }
public var snp_bottom: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Bottom) } public var snp_bottom: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Bottom) }
public var snp_leading: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Leading) } public var snp_leading: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Leading) }
public var snp_trailing: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Trailing) } public var snp_trailing: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Trailing) }
public var snp_width: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Width) } public var snp_width: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Width) }
public var snp_height: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Height) } public var snp_height: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Height) }
public var snp_centerX: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterX) } public var snp_centerX: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterX) }
public var snp_centerY: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterY) } public var snp_centerY: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterY) }
public var snp_baseline: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Baseline) } 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_leftMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeftMargin) }
public var snp_rightMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.RightMargin) } public var snp_rightMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.RightMargin) }
public var snp_topMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.TopMargin) } public var snp_topMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TopMargin) }
public var snp_bottomMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.BottomMargin) } public var snp_bottomMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.BottomMargin) }
public var snp_leadingMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.LeadingMargin) } public var snp_leadingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeadingMargin) }
public var snp_trailingMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.TrailingMargin) } public var snp_trailingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TrailingMargin) }
public var snp_centerXWithinMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterXWithinMargins) } public var snp_centerXWithinMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterXWithinMargins) }
public var snp_centerYWithinMargin: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterYWithinMargins) } 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_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) }
public var snp_size: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Size) } public var snp_size: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Size) }
public var snp_center: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Center) } 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_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) }
public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.CenterWithinMargins) } public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
public func snp_makeConstraints(block: (maker: ConstraintMaker) -> ()) { public func snp_makeConstraints(block: (maker: ConstraintMaker) -> ()) {
ConstraintMaker.makeConstraints(self, block: block) ConstraintMaker.makeConstraints(self, block: block)