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
50 changes: 12 additions & 38 deletions Sources/PinLayoutImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -594,43 +594,26 @@ class PinLayoutImpl: PinLayout {
//
@discardableResult
func size(_ size: CGSize) -> PinLayout {
if isSizeNotSet({ return "size(CGSize(width: \(size.width), height: \(size.height)))" }) {
width(size.width)
height(size.height)
}
return self
return setSize(size, { return "size(CGSize(width: \(size.width), height: \(size.height)))" })
}

@discardableResult
func size(_ sideLength: CGFloat) -> PinLayout {
if isSizeNotSet({ return "size(sideLength: \(sideLength))" }) {
width(sideLength)
height(sideLength)
}
return self
return setSize(CGSize(width: sideLength, height: sideLength), { return "size(sideLength: \(sideLength))" })
}

@discardableResult
func size(_ percent: Percent) -> PinLayout {
func context() -> String { return "size(\(percent))" }
if isSizeNotSet(context) {
guard let layoutSuperview = layoutSuperview(context) else { return self }
setWidth(percent.of(layoutSuperview.frame.width), context)
setHeight(percent.of(layoutSuperview.frame.height), context)
}
return self
guard let layoutSuperview = layoutSuperview(context) else { return self }
let size = CGSize(width: percent.of(layoutSuperview.frame.width), height: percent.of(layoutSuperview.frame.height))
return setSize(size, context)
}

@discardableResult
func size(of view: UIView) -> PinLayout {
func context() -> String { return "size(of \(view))" }
let viewSize = view.frame.size

if isSizeNotSet(context) {
setWidth(viewSize.width, context)
setHeight(viewSize.height, context)
}
return self
return setSize(view.frame.size, context)
}

@discardableResult
Expand Down Expand Up @@ -867,7 +850,7 @@ extension PinLayoutImpl {

if let _left = _left, let _right = _right {
warnConflict(context, ["left": _left, "right": _right])
} else if let width = width {
} else if let width = width, width != value {
warnPropertyAlreadySet("width", propertyValue: width, context)
} else {
width = value
Expand All @@ -883,27 +866,18 @@ extension PinLayoutImpl {

if let _top = _top, let _bottom = _bottom {
warnConflict(context, ["top": _top, "bottom": _bottom])
} else if let height = height {
} else if let height = height, height != value {
warnPropertyAlreadySet("height", propertyValue: height, context)
} else {
height = value
}
return self
}

fileprivate func isSizeNotSet(_ context: Context) -> Bool {
if let _top = _top, let _bottom = _bottom {
warnConflict(context, ["top": _top, "bottom": _bottom])
return false
} else if let height = height {
warnConflict(context, ["height": height])
return false
} else if let width = width {
warnConflict(context, ["width": width])
return false
} else {
return true
}
fileprivate func setSize(_ size: CGSize, _ context: Context) -> PinLayout {
setWidth(size.width, { return "\(context())'s width" })
setHeight(size.height, { return "\(context())'s height" })
return self
}

fileprivate func computeCoordinates(_ point: CGPoint, _ layoutSuperview: UIView, _ referenceView: UIView, _ referenceSuperview: UIView) -> CGPoint {
Expand Down
28 changes: 20 additions & 8 deletions Tests/AdjustSizeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class AdjustSizeSpec: QuickSpec {
}

beforeEach {
unitTestLastWarning = nil

viewController = UIViewController()

rootView = BasicView(text: "", color: .white)
Expand Down Expand Up @@ -169,29 +171,39 @@ class AdjustSizeSpec: QuickSpec {
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 200.0, height: 200.0)))
}

it("should warn that size() won't be applied") {
it("should warn that size()'s width won't be applied") {
aView.pin.width(90).size(CGSize(width: 25, height: 25))
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0)))
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 25.0)))
expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"]))
}

it("should warn that size()'s height won't be applied") {
aView.pin.height(90).size(CGSize(width: 25, height: 25))
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 25.0, height: 90.0)))
expect(unitTestLastWarning).to(contain(["size", "height", "won't be applied", "value has already been set"]))
}

it("should adjust the size of aView by calling a size(...) method") {
aView.pin.size(of: aViewChild)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 50.0, height: 30.0)))
}

it("should warn that size(of) won't be applied") {
it("should warn that size(of)'s width won't be applied") {
aView.pin.width(90).size(of: aViewChild)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0)))
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 30.0)))
expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"]))
}

it("should warn that size() won't be applied") {
it("should warn that size()'s width won't be applied") {
aView.pin.width(90).size(20)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0)))
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 20.0)))
expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"]))
}

it("should warn that size() won't be applied") {
it("should warn that size()'s width won't be applied") {
aView.pin.width(90).size(50%)
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0)))
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 200.0)))
expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"]))
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/PinEdgesSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class PinEdgesSpec: QuickSpec {
*/

beforeEach {
unitTestLastWarning = nil

viewController = UIViewController()

rootView = BasicView(text: "", color: .white)
Expand All @@ -54,8 +56,6 @@ class PinEdgesSpec: QuickSpec {
aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5))
aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100)
rootView.addSubview(aView)

unitTestLastWarning = nil
}

//
Expand Down