Skip to content

Commit 94d7efa

Browse files
author
Luc Dion
committed
Add Pin.activeWarnings.aspectRatioImageNotSet
1 parent 12aebab commit 94d7efa

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,8 @@ Few individual warnings can also be enabled/disabled individually:
15911591

15921592
* **`Pin.activeWarnings.noSpaceAvailableBetweenViews: Boolean`**
15931593
If true, a warning is displayed if there is no space available between views specified in a call to `horizontallyBetween(...)` or `verticallyBetween(...)`
1594+
* **`Pin.activeWarnings. aspectRatioImageNotSet: Boolean`**
1595+
If true, a warning is displayed if 'aspectRatio()' is called on a UIImageView without a valid UIImage.
15941596

15951597
<br/>
15961598

Sources/Pin.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ import Foundation
7676
}
7777

7878
@objc public class ActiveWarnings: NSObject {
79-
/// When set to true, a warning will be issue in Debug if there is no space available between views
80-
/// specified in a call to `horizontallyBetween(...)` or `verticallyBetween(...)`
79+
/// When set to true, a warning is displayed if there is no space available between views
80+
/// specified in a call to `horizontallyBetween(...)` or `verticallyBetween(...)`.
8181
public var noSpaceAvailableBetweenViews = true
82+
83+
/// When set to true, a warning is displayed if 'aspectRatio()' is called on a UIImageView without a valid UIImage.
84+
public var aspectRatioImageNotSet = true
8285
}

Sources/PinLayout+Between.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension PinLayout {
6666
setRight(view1MinX, context)
6767
applyVerticalAlignment(aligned, coordinates: [coordinates[1]], context: context)
6868
} else {
69-
guard Pin.activeWarnings.noSpaceAvailableBetweenViews else { return self }
69+
guard Pin.logWarnings && Pin.activeWarnings.noSpaceAvailableBetweenViews else { return self }
7070
warnWontBeApplied("there is no horizontal space between these views. (noSpaceAvailableBetweenViews)", context)
7171
}
7272

@@ -120,7 +120,7 @@ extension PinLayout {
120120
setBottom(view1MinY, context)
121121
applyHorizontalAlignment(aligned, coordinates: [coordinates[1]], context: context)
122122
} else {
123-
guard Pin.activeWarnings.noSpaceAvailableBetweenViews else { return self }
123+
guard Pin.logWarnings && Pin.activeWarnings.noSpaceAvailableBetweenViews else { return self }
124124
warnWontBeApplied("there is no vertical space between these views. (noSpaceAvailableBetweenViews)", context)
125125
}
126126

Sources/PinLayout+Size.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,13 @@ extension PinLayout {
144144
public func aspectRatio() -> PinLayout {
145145
func context() -> String { return "aspectRatio()" }
146146
guard let imageView = view as? UIImageView else {
147-
warnWontBeApplied("the layouted must be an UIImageView() to use the aspectRatio() method without parameter.", context)
147+
warnWontBeApplied("the layouted view must be an UIImageView() to use the aspectRatio() method without parameter.", context)
148148
return self
149149
}
150150

151151
guard let imageSize = imageView.image?.size else {
152-
warnWontBeApplied("the layouted UIImageView's image hasn't been set", context)
152+
guard Pin.logWarnings && Pin.activeWarnings.aspectRatioImageNotSet else { return self }
153+
warnWontBeApplied("the layouted UIImageView's image hasn't been set (aspectRatioImageNotSet)", context)
153154
return self
154155
}
155156

Tests/Common/AspectRatioTests.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class AspectRatioTests: QuickSpec {
6565
rootView.addSubview(imageView)
6666
#endif
6767
}
68+
69+
afterEach {
70+
_pinlayoutSetUnitTest(scale: nil)
71+
Pin.resetWarnings()
72+
}
6873

6974
//
7075
// aspectRatio(: CGFloat)
@@ -166,9 +171,27 @@ class AspectRatioTests: QuickSpec {
166171
describe("the result of the aspectRatio(CGFloat)") {
167172
it("should warn about aspectRatio()") {
168173
aView.pin.left().width(100).aspectRatio()
169-
expect(Pin.lastWarningText).to(contain(["aspectRatio() won't be applied", "the layouted must be an UIImageView()"]))
174+
expect(Pin.lastWarningText).to(contain(["aspectRatio() won't be applied", "the layouted view must be an UIImageView()"]))
170175
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 100.0, width: 100.0, height: 100.0)))
171176
}
177+
178+
it("should warn about aspectRatio() if no image is set") {
179+
imageView.image = nil
180+
imageView.pin.left().width(100).aspectRatio()
181+
expect(Pin.lastWarningText).to(contain(["aspectRatio() won't be applied", "the layouted UIImageView's image hasn't been set (aspectRatioImageNotSet)"]))
182+
}
183+
184+
it("should not warn about aspectRatio() if no image is set") {
185+
Pin.activeWarnings.aspectRatioImageNotSet = false
186+
imageView.image = nil
187+
imageView.pin.left().width(100).aspectRatio()
188+
expect(Pin.lastWarningText).to(beNil())
189+
expect(imageView.frame).to(equal(CGRect(x: 0.0, y: 10.0, width: 100.0, height: 60.0)))
190+
}
191+
192+
// guard Pin.logWarnings && else { return self }
193+
// warnWontBeApplied(")", context)
194+
172195

173196
it("should warn about aspectRatio()") {
174197
imageView.pin.left().aspectRatio()

0 commit comments

Comments
 (0)