Skip to content

Commit f57ae66

Browse files
author
Luc Dion
committed
size(…) methods now tries to apply width and height individually
Previously the size specified was applied only if both the width and height wasn’t specified. Now PinLayout will apply them individually, so if the width has been specified yet, the size’s width will be applied, else a warning will be displayed that indicate that the width won’t be applied. Same thing for the height. Plus: * Doesn’t display a warning anymore if the new specified width or height value is equal to the currently set value. This is coherent with other methods (top, left, hCenter, ….)
1 parent d5bf136 commit f57ae66

File tree

3 files changed

+34
-48
lines changed

3 files changed

+34
-48
lines changed

Sources/PinLayoutImpl.swift

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -594,43 +594,26 @@ class PinLayoutImpl: PinLayout {
594594
//
595595
@discardableResult
596596
func size(_ size: CGSize) -> PinLayout {
597-
if isSizeNotSet({ return "size(CGSize(width: \(size.width), height: \(size.height)))" }) {
598-
width(size.width)
599-
height(size.height)
600-
}
601-
return self
597+
return setSize(size, { return "size(CGSize(width: \(size.width), height: \(size.height)))" })
602598
}
603599

604600
@discardableResult
605601
func size(_ sideLength: CGFloat) -> PinLayout {
606-
if isSizeNotSet({ return "size(sideLength: \(sideLength))" }) {
607-
width(sideLength)
608-
height(sideLength)
609-
}
610-
return self
602+
return setSize(CGSize(width: sideLength, height: sideLength), { return "size(sideLength: \(sideLength))" })
611603
}
612604

613605
@discardableResult
614606
func size(_ percent: Percent) -> PinLayout {
615607
func context() -> String { return "size(\(percent))" }
616-
if isSizeNotSet(context) {
617-
guard let layoutSuperview = layoutSuperview(context) else { return self }
618-
setWidth(percent.of(layoutSuperview.frame.width), context)
619-
setHeight(percent.of(layoutSuperview.frame.height), context)
620-
}
621-
return self
608+
guard let layoutSuperview = layoutSuperview(context) else { return self }
609+
let size = CGSize(width: percent.of(layoutSuperview.frame.width), height: percent.of(layoutSuperview.frame.height))
610+
return setSize(size, context)
622611
}
623612

624613
@discardableResult
625614
func size(of view: UIView) -> PinLayout {
626615
func context() -> String { return "size(of \(view))" }
627-
let viewSize = view.frame.size
628-
629-
if isSizeNotSet(context) {
630-
setWidth(viewSize.width, context)
631-
setHeight(viewSize.height, context)
632-
}
633-
return self
616+
return setSize(view.frame.size, context)
634617
}
635618

636619
@discardableResult
@@ -867,7 +850,7 @@ extension PinLayoutImpl {
867850

868851
if let _left = _left, let _right = _right {
869852
warnConflict(context, ["left": _left, "right": _right])
870-
} else if let width = width {
853+
} else if let width = width, width != value {
871854
warnPropertyAlreadySet("width", propertyValue: width, context)
872855
} else {
873856
width = value
@@ -883,27 +866,18 @@ extension PinLayoutImpl {
883866

884867
if let _top = _top, let _bottom = _bottom {
885868
warnConflict(context, ["top": _top, "bottom": _bottom])
886-
} else if let height = height {
869+
} else if let height = height, height != value {
887870
warnPropertyAlreadySet("height", propertyValue: height, context)
888871
} else {
889872
height = value
890873
}
891874
return self
892875
}
893876

894-
fileprivate func isSizeNotSet(_ context: Context) -> Bool {
895-
if let _top = _top, let _bottom = _bottom {
896-
warnConflict(context, ["top": _top, "bottom": _bottom])
897-
return false
898-
} else if let height = height {
899-
warnConflict(context, ["height": height])
900-
return false
901-
} else if let width = width {
902-
warnConflict(context, ["width": width])
903-
return false
904-
} else {
905-
return true
906-
}
877+
fileprivate func setSize(_ size: CGSize, _ context: Context) -> PinLayout {
878+
setWidth(size.width, { return "\(context())'s width" })
879+
setHeight(size.height, { return "\(context())'s height" })
880+
return self
907881
}
908882

909883
fileprivate func computeCoordinates(_ point: CGPoint, _ layoutSuperview: UIView, _ referenceView: UIView, _ referenceSuperview: UIView) -> CGPoint {

Tests/AdjustSizeSpec.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class AdjustSizeSpec: QuickSpec {
5757
}
5858

5959
beforeEach {
60+
unitTestLastWarning = nil
61+
6062
viewController = UIViewController()
6163

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

172-
it("should warn that size() won't be applied") {
174+
it("should warn that size()'s width won't be applied") {
173175
aView.pin.width(90).size(CGSize(width: 25, height: 25))
174-
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 60.0)))
176+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 90.0, height: 25.0)))
177+
expect(unitTestLastWarning).to(contain(["size", "width", "won't be applied", "value has already been set"]))
178+
}
179+
180+
it("should warn that size()'s height won't be applied") {
181+
aView.pin.height(90).size(CGSize(width: 25, height: 25))
182+
expect(aView.frame).to(equal(CGRect(x: 140.0, y: 100.0, width: 25.0, height: 90.0)))
183+
expect(unitTestLastWarning).to(contain(["size", "height", "won't be applied", "value has already been set"]))
175184
}
176185

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

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

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

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

Tests/PinEdgesSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class PinEdgesSpec: QuickSpec {
4545
*/
4646

4747
beforeEach {
48+
unitTestLastWarning = nil
49+
4850
viewController = UIViewController()
4951

5052
rootView = BasicView(text: "", color: .white)
@@ -54,8 +56,6 @@ class PinEdgesSpec: QuickSpec {
5456
aView = BasicView(text: "View A", color: UIColor.red.withAlphaComponent(0.5))
5557
aView.frame = CGRect(x: 140, y: 100, width: 200, height: 100)
5658
rootView.addSubview(aView)
57-
58-
unitTestLastWarning = nil
5959
}
6060

6161
//

0 commit comments

Comments
 (0)