Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions Sources/Impl/PinLayoutImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -877,47 +877,133 @@ class PinLayoutImpl: PinLayout {
return self
}

@discardableResult
func marginTop(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginTop(\(percent.description))" }
return marginTop(percent, context)
}

@discardableResult
private func marginTop(_ percent: Percent, _ context: Context) -> Self {
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
marginTop = percent.of(layoutSuperviewRect.height)
return self
}

@discardableResult
func marginLeft(_ value: CGFloat) -> PinLayout {
marginLeft = value
return self
}

@discardableResult
func marginLeft(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginLeft(\(percent.description))" }
return marginLeft(percent, context)
}

@discardableResult
private func marginLeft(_ percent: Percent, _ context: Context) -> Self {
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
marginLeft = percent.of(layoutSuperviewRect.width)
return self
}

@discardableResult
func marginBottom(_ value: CGFloat) -> PinLayout {
marginBottom = value
return self
}

@discardableResult
func marginBottom(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginBottom(\(percent.description))" }
return marginBottom(percent, context)
}

@discardableResult
private func marginBottom(_ percent: Percent, _ context: Context) -> Self {
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
marginBottom = percent.of(layoutSuperviewRect.height)
return self
}

@discardableResult
func marginRight(_ value: CGFloat) -> PinLayout {
marginRight = value
return self
}

@discardableResult
func marginRight(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginRight(\(percent.description))" }
return marginRight(percent, context)
}

@discardableResult
private func marginRight(_ percent: Percent, _ context: Context) -> Self {
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
marginRight = percent.of(layoutSuperviewRect.width)
return self
}

@discardableResult
func marginStart(_ value: CGFloat) -> PinLayout {
return isLTR() ? marginLeft(value) : marginRight(value)
}

@discardableResult
func marginStart(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginStart(\(percent.description))" }
return isLTR() ? marginLeft(percent, context) : marginRight(percent, context)
}

@discardableResult
func marginEnd(_ value: CGFloat) -> PinLayout {
return isLTR() ? marginRight(value) : marginLeft(value)
}

@discardableResult
func marginEnd(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginEnd(\(percent.description))" }
return isLTR() ? marginRight(percent, context) : marginLeft(percent, context)
}

@discardableResult
func marginHorizontal(_ value: CGFloat) -> PinLayout {
marginLeft = value
marginRight = value
return self
}

@discardableResult
func marginHorizontal(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginHorizontal(\(percent.description))" }
return marginHorizontal(percent, context)
}

@discardableResult
private func marginHorizontal(_ percent: Percent, _ context: Context) -> Self {
return marginLeft(percent, context).marginRight(percent, context)
}

@discardableResult
func marginVertical(_ value: CGFloat) -> PinLayout {
marginTop = value
marginBottom = value
return self
}

@discardableResult
func marginVertical(_ percent: Percent) -> PinLayout {
func context() -> String { return "marginVertical(\(percent.description))" }
return marginVertical(percent, context)
}

@discardableResult
private func marginVertical(_ percent: Percent, _ context: Context) -> Self {
return marginTop(percent, context).marginBottom(percent, context)
}

@discardableResult
func margin(_ insets: UIEdgeInsets) -> PinLayout {
Expand Down Expand Up @@ -947,6 +1033,15 @@ class PinLayoutImpl: PinLayout {
return self
}

@discardableResult
func margin(_ percent: Percent) -> PinLayout {
func context() -> String { return "margin(\(percent.description))" }
return marginTop(percent, context)
.marginLeft(percent, context)
.marginBottom(percent, context)
.marginRight(percent, context)
}

@discardableResult
func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout {
marginTop = top
Expand All @@ -956,6 +1051,17 @@ class PinLayoutImpl: PinLayout {
return self
}

@discardableResult
func margin(_ top: Percent, _ left: Percent, _ bottom: Percent, _ right: Percent) -> PinLayout {
func context() -> String {
return "margin(top: \(top.description), left: \(left.description), bottom: \(bottom.description), right: \(right.description)"
}
return marginTop(top, context)
.marginLeft(left, context)
.marginBottom(bottom, context)
.marginRight(right, context)
}

@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout {
marginTop = vertical
marginLeft = horizontal
Expand All @@ -964,6 +1070,12 @@ class PinLayoutImpl: PinLayout {
return self
}

@discardableResult
func margin(_ vertical: Percent, _ horizontal: Percent) -> PinLayout {
func context() -> String { return "margin(vertical: \(vertical.description), horizontal: \(horizontal.description)"}
return marginVertical(vertical, context).marginHorizontal(horizontal, context)
}

@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout {
marginTop = top
marginLeft = horizontal
Expand All @@ -972,6 +1084,12 @@ class PinLayoutImpl: PinLayout {
return self
}

@discardableResult
func margin(_ top: Percent, _ horizontal: Percent, _ bottom: Percent) -> PinLayout {
func context() -> String { return "margin(top: \(top.description), horizontal: \(horizontal.description), bottom: \(bottom.description)"}
return marginTop(top, context).marginHorizontal(horizontal, context).marginBottom(bottom, context)
}

@discardableResult
func pinEdges() -> PinLayout {
shouldPinEdges = true
Expand Down
24 changes: 24 additions & 0 deletions Sources/PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,21 +423,29 @@ public protocol PinLayout {
Set the top margin.
*/
@discardableResult func marginTop(_ value: CGFloat) -> PinLayout

@discardableResult func marginTop(_ percent: Percent) -> PinLayout

/**
Set the left margin.
*/
@discardableResult func marginLeft(_ value: CGFloat) -> PinLayout

@discardableResult func marginLeft(_ percent: Percent) -> PinLayout

/**
Set the bottom margin.
*/
@discardableResult func marginBottom(_ value: CGFloat) -> PinLayout

@discardableResult func marginBottom(_ percent: Percent) -> PinLayout

/**
Set the right margin.
*/
@discardableResult func marginRight(_ value: CGFloat) -> PinLayout

@discardableResult func marginRight(_ percent: Percent) -> PinLayout

// RTL support
/**
Expand All @@ -448,6 +456,8 @@ public protocol PinLayout {
* In RTL direction, start margin specify the **right** margin.
*/
@discardableResult func marginStart(_ value: CGFloat) -> PinLayout

@discardableResult func marginStart(_ percent: Percent) -> PinLayout

/**
Set the end margin.
Expand All @@ -457,17 +467,23 @@ public protocol PinLayout {
* In RTL direction, end margin specify the **left** margin.
*/
@discardableResult func marginEnd(_ value: CGFloat) -> PinLayout

@discardableResult func marginEnd(_ percent: Percent) -> PinLayout

/**
Set the left, right, start and end margins to the specified value.
*/
@discardableResult func marginHorizontal(_ value: CGFloat) -> PinLayout

@discardableResult func marginHorizontal(_ percent: Percent) -> PinLayout

/**
Set the top and bottom margins to the specified value.
*/
@discardableResult func marginVertical(_ value: CGFloat) -> PinLayout

@discardableResult func marginVertical(_ percent: Percent) -> PinLayout

/**
Set all margins using UIEdgeInsets.
This method is particularly useful to set all margins using iOS 11 `UIView.safeAreaInsets`.
Expand All @@ -487,22 +503,30 @@ public protocol PinLayout {
Set all margins to the specified value.
*/
@discardableResult func margin(_ value: CGFloat) -> PinLayout

@discardableResult func margin(_ percent: Percent) -> PinLayout

/**
Set individually vertical margins (top, bottom) and horizontal margins (left, right, start, end).
*/
@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout

@discardableResult func margin(_ vertical: Percent, _ horizontal: Percent) -> PinLayout

/**
Set individually top, horizontal margins and bottom margin.
*/
@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout

@discardableResult func margin(_ top: Percent, _ horizontal: Percent, _ bottom: Percent) -> PinLayout

/**
Set individually top, left, bottom and right margins.
*/
@discardableResult func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout

@discardableResult func margin(_ top: Percent, _ left: Percent, _ bottom: Percent, _ right: Percent) -> PinLayout

/// Normally if only either left or right has been specified, PinLayout will MOVE the view to apply left or right margins.
/// This is also true even if the width has been set.
/// Calling pinEdges() will force PinLayout to pin the four edges and then apply left and/or right margins, and this without
Expand Down
Loading