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
20 changes: 20 additions & 0 deletions Sources/Impl/PinLayoutImpl+Coordinates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ extension PinLayoutImpl {
internal func top(_ context: Context) {
setTop(0, context)
}

@discardableResult
internal func top(_ value: CGFloat, _ context: Context) -> PinLayout {
setTop(value, context)

@lucdion lucdion Jan 11, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to add this method if you call setTop(value, context), setBottom(value, context) ... in func all(_ value: CGFloat),horizontally(...) and vertically(...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok you can keep it that way, I use this pattern somewhere else 😊

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But could you update the method func top(_ value: CGFloat) -> PinLayout in PinLayoutImpl.swift to:

@discardableResult
    func top(_ value: CGFloat) -> PinLayout {
        return top(value, { return "top(\(value))" })
    }

return self
}

@discardableResult
internal func top(_ percent: Percent, _ context: Context) -> PinLayout {
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the method func top(_ percent: Percent)-> PinLayout in PinLayoutImpl.swift to:

func top(_ percent: Percent) -> PinLayout {
        return top(percent,  { return "top(\(percent.description))" })
    }

setTop(percent.of(layoutSuperviewRect.height), context)
return self
}

internal func setTop(_ value: CGFloat, _ context: Context) {
if let _bottom = _bottom, let height = height {
Expand Down Expand Up @@ -134,6 +147,13 @@ extension PinLayoutImpl {
setBottom(layoutSuperviewRect.height - value, context)
return self
}

@discardableResult
internal func bottom(_ percent: Percent, _ context: Context) -> PinLayout {
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the method func bottom(_ percent: Percent) -> PinLayout in PinLayoutImpl.swift to:

func bottom(_ percent: Percent) -> PinLayout {
        return bottom(percent, { return "bottom(\(percent.description))" })
    }

setBottom(layoutSuperviewRect.height - percent.of(layoutSuperviewRect.height), context)
return self
}

internal func setBottom(_ value: CGFloat, _ context: Context) {
if let _top = _top, let height = height {
Expand Down
46 changes: 40 additions & 6 deletions Sources/Impl/PinLayoutImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class PinLayoutImpl: PinLayout {

@discardableResult
func top(_ value: CGFloat) -> PinLayout {
setTop(value, { return "top(\(value))" })
return self
return top(value, { return "top(\(value))" })
}

@discardableResult
Expand Down Expand Up @@ -141,10 +140,7 @@ class PinLayoutImpl: PinLayout {

@discardableResult
func bottom(_ percent: Percent) -> PinLayout {
func context() -> String { return "bottom(\(percent.description))" }
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
bottom(percent.of(layoutSuperviewRect.height), context)
return self
return bottom(percent, { return "bottom(\(percent.description))" })
}

@discardableResult func right() -> PinLayout {
Expand Down Expand Up @@ -234,20 +230,58 @@ class PinLayoutImpl: PinLayout {
left({ "all() left coordinate" })
return self
}

@discardableResult
func all(_ value: CGFloat) -> PinLayout {
top(value, { "all(\(value)) top coordinate" })
bottom(value, { "all(\(value)) bottom coordinate" })
left(value, { "all(\(value)) left coordinate" })
right(value, { "all(\(value)) right coordinate" })
return self
}

@discardableResult
func horizontally() -> PinLayout {
right({ "horizontally() right coordinate" })
left({ "horizontally() left coordinate" })
return self
}

@discardableResult
func horizontally(_ value: CGFloat) -> PinLayout {
left(value, { return "horizontally(\(value)) left coordinate" })
right(value, { return "horizontally(\(value)) right coordinate" })
return self
}

@discardableResult
func horizontally(_ percent: Percent) -> PinLayout {
left(percent, { return "horizontally(\(percent.description)) left coordinate" })
right(percent, { return "horizontally(\(percent.description)) right coordinate" })
return self
}

@discardableResult
func vertically() -> PinLayout {
top({ "vertically() top coordinate" })
bottom({ "vertically() bottom coordinate" })
return self
}

@discardableResult
func vertically(_ value: CGFloat) -> PinLayout {
top(value, { return "vertically(\(value)) top coordinate" })
bottom(value, { return "vertically(\(value)) bottom coordinate" })
return self
}

@discardableResult
func vertically(_ percent: Percent) -> PinLayout {
top(percent, { return "vertically(\(percent.description)) top coordinate" })
bottom(percent, { return "vertically(\(percent.description)) bottom coordinate" })
return self
}

//
// top, left, bottom, right
//
Expand Down
10 changes: 10 additions & 0 deletions Sources/ObjectiveC/PinLayoutObjCImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,21 @@ import UIKit
impl?.horizontally()
return self
}

public func horizontally(_ value: CGFloat) -> PinLayoutObjC {
impl?.horizontally(value)
return self
}

public func vertically() -> PinLayoutObjC {
impl?.vertically()
return self
}

public func vertically(_ value: CGFloat) -> PinLayoutObjC {
impl?.vertically(value)
return self
}

public func topLeft(to anchor: Anchor) -> PinLayoutObjC {
impl?.topLeft(to: anchor)
Expand Down
5 changes: 5 additions & 0 deletions Sources/PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,23 @@ public protocol PinLayout {
Similar to calling `view.top().bottom().left().right()`
*/
@discardableResult func all() -> PinLayout
@discardableResult func all(_ value: CGFloat) -> PinLayout
/**
Pin the left and right edges on its superview's corresponding edges.

Similar to calling `view.left().right()`.
*/
@discardableResult func horizontally() -> PinLayout
@discardableResult func horizontally(_ value: CGFloat) -> PinLayout
@discardableResult func horizontally(_ percent: Percent) -> PinLayout
/**
Pin the **top and bottom edges** on its superview's corresponding edges.

Similar to calling `view.top().bottom()`.
*/
@discardableResult func vertically() -> PinLayout
@discardableResult func vertically(_ value: CGFloat) -> PinLayout
@discardableResult func vertically(_ percent: Percent) -> PinLayout

//
// MARK: Layout using edges
Expand Down
72 changes: 72 additions & 0 deletions Tests/PinEdgesSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ class PinEdgesSpec: QuickSpec {
aView.pin.all().margin(10)
expect(aView.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 380.0, height: 380.0)))
}

it("should adjust the aView") {
aView.pin.all(10)
expect(aView.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 380.0, height: 380.0)))
}

it("should adjust the bViewChild") {
bViewChild.pin.all()
Expand All @@ -479,6 +484,11 @@ class PinEdgesSpec: QuickSpec {
bViewChild.pin.all().margin(10)
expect(bViewChild.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 90.0, height: 60.0)))
}

it("should adjust the bViewChild") {
bViewChild.pin.all(10)
expect(bViewChild.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 90.0, height: 60.0)))
}

it("should warn") {
aView.pin.top(20).all()
Expand Down Expand Up @@ -512,6 +522,16 @@ class PinEdgesSpec: QuickSpec {
aView.pin.horizontally().margin(10)
expect(aView.frame).to(equal(CGRect(x: 10.0, y: 100.0, width: 380.0, height: 100.0)))
}

it("should adjust the aView") {
aView.pin.horizontally(10)
expect(aView.frame).to(equal(CGRect(x: 10.0, y: 100.0, width: 380.0, height: 100.0)))
}

it("should adjust the aView") {
aView.pin.horizontally(10%)
expect(aView.frame).to(equal(CGRect(x: 40.0, y: 100.0, width: 320.0, height: 100.0)))
}

it("should adjust the bViewChild") {
bViewChild.pin.horizontally()
Expand All @@ -522,6 +542,16 @@ class PinEdgesSpec: QuickSpec {
bViewChild.pin.horizontally().margin(10)
expect(bViewChild.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 90.0, height: 20.0)))
}

it("should adjust the bViewChild") {
bViewChild.pin.horizontally(10)
expect(bViewChild.frame).to(equal(CGRect(x: 10.0, y: 10.0, width: 90.0, height: 20.0)))
}

it("should adjust the bViewChild") {
bViewChild.pin.horizontally(10%)
expect(bViewChild.frame).to(equal(CGRect(x: 11.0, y: 10.0, width: 88.0, height: 20.0)))
}

it("should warn") {
aView.pin.left(20).horizontally()
Expand All @@ -531,6 +561,22 @@ class PinEdgesSpec: QuickSpec {
aView.pin.right(20).horizontally()
expect(Pin.lastWarningText).to(contain(["horizontally() right coordinate", "won't be applied", "already been set to 20"]))
}
it("should warn") {
aView.pin.left(10).horizontally(20)
expect(Pin.lastWarningText).to(contain(["horizontally(20.0) left coordinate", "won't be applied", "already been set to 10"]))
}
it("should warn") {
aView.pin.right(10).horizontally(20)
expect(Pin.lastWarningText).to(contain(["horizontally(20.0) right coordinate", "won't be applied", "already been set to 10"]))
}
it("should warn") {
aView.pin.left(10).horizontally(10%)
expect(Pin.lastWarningText).to(contain(["horizontally(10%) left coordinate", "won't be applied", "already been set to 10"]))
}
it("should warn") {
aView.pin.right(10).horizontally(10%)
expect(Pin.lastWarningText).to(contain(["horizontally(10%) right coordinate", "won't be applied", "already been set to 10"]))
}
}

//
Expand All @@ -546,6 +592,16 @@ class PinEdgesSpec: QuickSpec {
aView.pin.vertically().margin(10)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 10.0, width: 200.0, height: 380.0)))
}

it("should adjust the aView") {
aView.pin.vertically(10)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 10.0, width: 200.0, height: 380.0)))
}

it("should adjust the aView") {
aView.pin.vertically(10%)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 40.0, width: 200.0, height: 320.0)))
}

it("should adjust the bViewChild") {
bViewChild.pin.vertically()
Expand All @@ -565,6 +621,22 @@ class PinEdgesSpec: QuickSpec {
aView.pin.bottom(20).vertically()
expect(Pin.lastWarningText).to(contain(["vertically() bottom coordinate", "won't be applied", "already been set to 20"]))
}
it("should warn") {
aView.pin.top(10).vertically(20)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you have updated the method context, thanks to updating expectations, here it should contain top.

expect(Pin.lastWarningText).to(contain(["vertically(20.0) top coordinate", "won't be applied", "already been set to 10"]))
}
it("should warn") {
aView.pin.bottom(10).vertically(20)
expect(Pin.lastWarningText).to(contain(["vertically(20.0) bottom coordinate", "won't be applied", "already been set to 10"]))
}
it("should warn") {
aView.pin.top(10).vertically(10%)
expect(Pin.lastWarningText).to(contain(["vertically(10%) top coordinate", "won't be applied", "already been set to 10"]))
}
it("should warn") {
aView.pin.bottom(10).vertically(10%)
expect(Pin.lastWarningText).to(contain(["vertically(10%) bottom coordinate", "won't be applied", "already been set to 10"]))
}
}
}
}