Skip to content

Commit

Permalink
Merge pull request #18 from jduff/add-dimension-multiplier-support
Browse files Browse the repository at this point in the history
Adds the ability to specify a multiplier when pinning dimensions
  • Loading branch information
joshpc authored Jun 20, 2017
2 parents 889a35a + 8cbae2a commit bc30395
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 46 deletions.
78 changes: 34 additions & 44 deletions Pins/View+Pins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public extension Pinnable {
#endif

public extension Pinnable where Self: Pinnable {
// MARK: Convenience Pinning Methods

/// Pin view boundries to the specified anchors. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
///
/// - Parameters:
Expand Down Expand Up @@ -243,6 +245,8 @@ public extension Pinnable where Self: Pinnable {
pin(.bottom, to: bottom, padding: padding)]
}

// MARK: Horizontal Anchors

/// Pin the specified `HorizontalAnchor` of the view equal to another anchor. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
///
/// - Parameters:
Expand Down Expand Up @@ -289,7 +293,6 @@ public extension Pinnable where Self: Pinnable {
return disableTranslatesAutoresizingMaskAndActivate(constraint)
}


/// Pin the specified `HorizontalAnchor` of the view equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
///
/// - Parameters:
Expand All @@ -299,10 +302,7 @@ public extension Pinnable where Self: Pinnable {
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ edge: HorizontalAnchor, to view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: edge).constraint(equalTo: anchor(view, for: edge),
constant: convertPadding(edge, padding))

return disableTranslatesAutoresizingMaskAndActivate(constraint)
return pin(edge, to: anchor(view, for: edge), padding: padding)
}

/// Pin the specified `HorizontalAnchor` of the view less than or equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand All @@ -314,10 +314,7 @@ public extension Pinnable where Self: Pinnable {
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ edge: HorizontalAnchor, lessThanOrEqualTo view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: edge).constraint(lessThanOrEqualTo: anchor(view, for: edge),
constant: convertPadding(edge, padding))

return disableTranslatesAutoresizingMaskAndActivate(constraint)
return pin(edge, lessThanOrEqualTo: anchor(view, for: edge), padding: padding)
}

/// Pin the specified `HorizontalAnchor` of the view greater than equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand All @@ -329,11 +326,10 @@ public extension Pinnable where Self: Pinnable {
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ edge: HorizontalAnchor, greaterThanOrEqualTo view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: edge).constraint(greaterThanOrEqualTo: anchor(view, for: edge),
constant: convertPadding(edge, padding))

return disableTranslatesAutoresizingMaskAndActivate(constraint)
return pin(edge, greaterThanOrEqualTo: anchor(view, for: edge), padding: padding)
}

// MARK: Vertical Anchors

/// Pin the specified `VerticalAnchor` of the view equal to another anchor. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
///
Expand Down Expand Up @@ -389,10 +385,7 @@ public extension Pinnable where Self: Pinnable {
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ edge: VerticalAnchor, to view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: edge).constraint(equalTo: anchor(view, for: edge),
constant: convertPadding(edge, padding))

return disableTranslatesAutoresizingMaskAndActivate(constraint)
return pin(edge, to: anchor(view, for: edge), padding: padding)
}

/// Pin the specified `VerticalAnchor` of the view less than or equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand All @@ -404,10 +397,7 @@ public extension Pinnable where Self: Pinnable {
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ edge: VerticalAnchor, lessThanOrEqualTo view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: edge).constraint(lessThanOrEqualTo: anchor(view, for: edge),
constant: convertPadding(edge, padding))

return disableTranslatesAutoresizingMaskAndActivate(constraint)
return pin(edge, lessThanOrEqualTo: anchor(view, for: edge), padding: padding)
}

/// Pin the specified `VerticalAnchor` of the view greater than or equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand All @@ -419,22 +409,22 @@ public extension Pinnable where Self: Pinnable {
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ edge: VerticalAnchor, greaterThanOrEqualTo view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: edge).constraint(greaterThanOrEqualTo: anchor(view, for: edge),
constant: convertPadding(edge, padding))

return disableTranslatesAutoresizingMaskAndActivate(constraint)
return pin(edge, greaterThanOrEqualTo: anchor(view, for: edge), padding: padding)
}

// MARK: Dimension Anchors

/// Pin the specified `DimensionAnchor` of the view to another anchor. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
///
/// - Parameters:
/// - dimension: `DimensionAnchor` of the caller to pin to. Either `width` or `height`.
/// - anchorAttachment: Anchor to pin the view to. Must be a `NSLayoutDimension`.
/// - padding: Optional padding to add between the anchors.
/// - multiplier: Optional multiplier with a default value of 1.0. When used, the result will be anchorValue * multiplier + constant
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin(_ dimension: DimensionAnchor, to anchorAttachment: NSLayoutAnchor<NSLayoutDimension>, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(equalTo: anchorAttachment, constant: padding)
public func pin(_ dimension: DimensionAnchor, to anchorAttachment: NSLayoutAnchor<NSLayoutDimension>, padding: CGFloat = 0.0, multiplier: CGFloat = 1.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(equalTo: anchorAttachment as! NSLayoutDimension, multiplier: multiplier, constant: padding)

return disableTranslatesAutoresizingMaskAndActivate(constraint)
}
Expand All @@ -445,10 +435,11 @@ public extension Pinnable where Self: Pinnable {
/// - dimension: `DimensionAnchor` of the caller to pin to. Either `width` or `height`.
/// - anchorAttachment: Anchor to pin the view to. Must be a `NSLayoutDimension`.
/// - padding: Optional padding to add between the anchors.
/// - multiplier: Optional multiplier with a default value of 1.0. When used, the result will be anchorValue * multiplier + constant
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin(_ dimension: DimensionAnchor, lessThanOrEqualTo anchorAttachment: NSLayoutAnchor<NSLayoutDimension>, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(lessThanOrEqualTo: anchorAttachment, constant: padding)
public func pin(_ dimension: DimensionAnchor, lessThanOrEqualTo anchorAttachment: NSLayoutAnchor<NSLayoutDimension>, padding: CGFloat = 0.0, multiplier: CGFloat = 1.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(lessThanOrEqualTo: anchorAttachment as! NSLayoutDimension, multiplier: multiplier, constant: padding)

return disableTranslatesAutoresizingMaskAndActivate(constraint)
}
Expand All @@ -459,10 +450,11 @@ public extension Pinnable where Self: Pinnable {
/// - dimension: `DimensionAnchor` of the caller to pin to. Either `width` or `height`.
/// - anchorAttachment: Anchor to pin the view to. Must be a `NSLayoutDimension`.
/// - padding: Optional padding to add between the anchors.
/// - multiplier: Optional multiplier with a default value of 1.0. When used, the result will be anchorValue * multiplier + constant
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin(_ dimension: DimensionAnchor, greaterThanOrEqualTo anchorAttachment: NSLayoutAnchor<NSLayoutDimension>, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(greaterThanOrEqualTo: anchorAttachment, constant: padding)
public func pin(_ dimension: DimensionAnchor, greaterThanOrEqualTo anchorAttachment: NSLayoutAnchor<NSLayoutDimension>, padding: CGFloat = 0.0, multiplier: CGFloat = 1.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(greaterThanOrEqualTo: anchorAttachment as! NSLayoutDimension, multiplier: multiplier, constant: padding)

return disableTranslatesAutoresizingMaskAndActivate(constraint)
}
Expand All @@ -473,12 +465,11 @@ public extension Pinnable where Self: Pinnable {
/// - dimension: `DimensionAnchor` of the caller to pin to. Either `width` or `height`.
/// - view: View to pin the caller to. Pins to the same anchor as `edge`. Must be a `View` or `LayoutGuide`.
/// - padding: Optional padding to add between the anchors.
/// - multiplier: Optional multiplier with a default value of 1.0. When used, the result will be anchorValue * multiplier + constant
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ dimension: DimensionAnchor, to view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(equalTo: anchor(view, for: dimension), constant: padding)

return disableTranslatesAutoresizingMaskAndActivate(constraint)
public func pin<View: Pinnable>(_ dimension: DimensionAnchor, to view: View, padding: CGFloat = 0.0, multiplier: CGFloat = 1.0) -> NSLayoutConstraint {
return pin(dimension, to: anchor(view, for: dimension), padding: padding, multiplier: multiplier)
}

/// Pin the specified `DimensionAnchor` of the view less than or equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand All @@ -487,12 +478,11 @@ public extension Pinnable where Self: Pinnable {
/// - dimension: `DimensionAnchor` of the caller to pin to. Either `width` or `height`.
/// - view: View to pin the caller to. Pins to the same anchor as `edge`. Must be a `View` or `LayoutGuide`.
/// - padding: Optional padding to add between the anchors.
/// - multiplier: Optional multiplier with a default value of 1.0. When used, the result will be anchorValue * multiplier + constant
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ dimension: DimensionAnchor, lessThanOrEqualTo view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(lessThanOrEqualTo: anchor(view, for: dimension), constant: padding)

return disableTranslatesAutoresizingMaskAndActivate(constraint)
public func pin<View: Pinnable>(_ dimension: DimensionAnchor, lessThanOrEqualTo view: View, padding: CGFloat = 0.0, multiplier: CGFloat = 1.0) -> NSLayoutConstraint {
return pin(dimension, lessThanOrEqualTo: anchor(view, for: dimension), padding: padding, multiplier: multiplier)
}

/// Pin the specified `DimensionAnchor` of the view greater than or equal to the same anchor on another view. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand All @@ -501,12 +491,11 @@ public extension Pinnable where Self: Pinnable {
/// - dimension: `DimensionAnchor` of the caller to pin to. Either `width` or `height`.
/// - view: View to pin the caller to. Pins to the same anchor as `edge`. Must be a `View` or `LayoutGuide`.
/// - padding: Optional padding to add between the anchors.
/// - multiplier: Optional multiplier with a default value of 1.0. When used, the result will be anchorValue * multiplier + constant
/// - Returns: The activated `NSLayoutConstraint` object that was created.
@discardableResult
public func pin<View: Pinnable>(_ dimension: DimensionAnchor, greaterThanOrEqualTo view: View, padding: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = anchor(self, for: dimension).constraint(greaterThanOrEqualTo: anchor(view, for: dimension), constant: padding)

return disableTranslatesAutoresizingMaskAndActivate(constraint)
public func pin<View: Pinnable>(_ dimension: DimensionAnchor, greaterThanOrEqualTo view: View, padding: CGFloat = 0.0, multiplier: CGFloat = 1.0) -> NSLayoutConstraint {
return pin(dimension, greaterThanOrEqualTo: anchor(view, for: dimension), padding: padding, multiplier: multiplier)
}

/// Pin the specified `DimensionAnchor` of the view equal to a fixed size. Calling this method sets `translatesAutoresizingMaskIntoConstraints` to `false` on the caller.
Expand Down Expand Up @@ -588,7 +577,8 @@ public extension Pinnable where Self: Pinnable {
return constraints
}

// MARK: Private helper methods.
// MARK: Private Helper Methods

private func anchor<View: Pinnable>(_ view: View, for anchor: HorizontalAnchor) -> NSLayoutAnchor<NSLayoutXAxisAnchor> {
switch anchor {
case .leading:
Expand Down
Loading

0 comments on commit bc30395

Please sign in to comment.