Skip to content

Commit 0eb3420

Browse files
authored
Merge pull request #115 from mirego/fix_uiscrollview_layout
Fix UIScrollView layout
2 parents 54c0451 + 9321eca commit 0eb3420

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

PinLayout.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
CA6C7C85508320B18B1FC777 /* Pods_PinLayoutTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 45CE7ABE76F48416DE57DF55 /* Pods_PinLayoutTests.framework */; };
5959
DF7A36BD1E918301000F9856 /* PinEdgesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF7A36BC1E918301000F9856 /* PinEdgesSpec.swift */; };
6060
DFC97CA71E8A8F2C001545D5 /* PinLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFC97CA61E8A8F2C001545D5 /* PinLayout.swift */; };
61+
DFCA5F1620111E0B00180CD7 /* UIScrollViewSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DFCA5F1420111BCF00180CD7 /* UIScrollViewSpec.swift */; };
6162
/* End PBXBuildFile section */
6263

6364
/* Begin PBXContainerItemProxy section */
@@ -128,6 +129,7 @@
128129
6070E55EDEAA3DC58C0B4CDD /* Pods-PinLayoutTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PinLayoutTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PinLayoutTests/Pods-PinLayoutTests.debug.xcconfig"; sourceTree = "<group>"; };
129130
DF7A36BC1E918301000F9856 /* PinEdgesSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinEdgesSpec.swift; sourceTree = "<group>"; };
130131
DFC97CA61E8A8F2C001545D5 /* PinLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinLayout.swift; sourceTree = "<group>"; };
132+
DFCA5F1420111BCF00180CD7 /* UIScrollViewSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIScrollViewSpec.swift; sourceTree = "<group>"; };
131133
/* End PBXFileReference section */
132134

133135
/* Begin PBXFrameworksBuildPhase section */
@@ -240,6 +242,7 @@
240242
2482908B1E78CFFC00667D08 /* RelativePositionSpec.swift */,
241243
242E8DC11EED5982005935FB /* RelativePositionMultipleViewsSpec.swift */,
242244
245C1DA11FEC4FC6007594F7 /* TransformSpec.swift */,
245+
DFCA5F1420111BCF00180CD7 /* UIScrollViewSpec.swift */,
243246
240F88BF1F0C1ED900280FC8 /* WarningSpec.swift */,
244247
248E4C751F7A88CF00C0E7F7 /* UIImage+Color.swift */,
245248
);
@@ -525,6 +528,7 @@
525528
248E4C741F7A883800C0E7F7 /* AspectRatioTests.swift in Sources */,
526529
240F88C11F0C1F5000280FC8 /* WarningSpec.swift in Sources */,
527530
245C1DA31FEC4FCC007594F7 /* TransformSpec.swift in Sources */,
531+
DFCA5F1620111E0B00180CD7 /* UIScrollViewSpec.swift in Sources */,
528532
242E8DC31EED5AB2005935FB /* RelativePositionMultipleViewsSpec.swift in Sources */,
529533
2469C5041E75DB7600073BEE /* RectNimbleMatcher.swift in Sources */,
530534
242723731F008BF7006A5C3A /* MinMaxWidthHeightSpec.swift in Sources */,

Sources/Impl/Coordinates.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ class Coordinates {
9898
// NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account.
9999
view.center = CGPoint(x: adjustedRect.origin.x + (adjustedRect.width * view.layer.anchorPoint.x),
100100
y: adjustedRect.origin.y + (adjustedRect.height * view.layer.anchorPoint.y))
101-
view.bounds = CGRect(origin: .zero, size: adjustedRect.size)
102-
}
101+
// NOTE: We must set only the bounds's size and keep the origin.
102+
view.bounds.size = adjustedRect.size
103+
}
103104

104105
static func getUntransformedViewRect(_ view: UIView) -> CGRect {
105106
/*

Tests/UIScrollViewSpec.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2017 Luc Dion
2+
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
// of this software and associated documentation files (the "Software"), to deal
4+
// in the Software without restriction, including without limitation the rights
5+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6+
// copies of the Software, and to permit persons to whom the Software is
7+
// furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in
10+
// all copies or substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
// THE SOFTWARE.
19+
20+
import Quick
21+
import Nimble
22+
import PinLayout
23+
24+
class UIScrollViewSpec: QuickSpec {
25+
override func spec() {
26+
var viewController: UIViewController!
27+
28+
var rootView: BasicView!
29+
var scrollView: UIScrollView!
30+
31+
/*
32+
root
33+
|
34+
|- scrollView
35+
*/
36+
37+
beforeEach {
38+
viewController = UIViewController()
39+
40+
rootView = BasicView(text: "", color: .white)
41+
viewController.view.addSubview(rootView)
42+
43+
scrollView = UIScrollView()
44+
rootView.addSubview(scrollView)
45+
46+
rootView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
47+
scrollView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
48+
}
49+
50+
describe("Layout a UIScrollView") {
51+
it("layout the UIScrollView") {
52+
scrollView.pin.all()
53+
54+
expect(scrollView.frame).to(equal(CGRect(x: 0, y: 0, width: 400.0, height: 400.0)))
55+
expect(scrollView.bounds).to(equal(CGRect(x: 0, y: 0, width: 400.0, height: 400.0)))
56+
expect(scrollView.contentOffset).to(equal(CGPoint(x: 0, y: 0)))
57+
}
58+
59+
it("should keep the contentOffset") {
60+
scrollView.contentOffset = CGPoint(x: 30, y: 30)
61+
62+
scrollView.pin.all()
63+
64+
expect(scrollView.frame).to(equal(CGRect(x: 0, y: 0, width: 400.0, height: 400.0)))
65+
expect(scrollView.bounds).to(equal(CGRect(x: 30, y: 30, width: 400.0, height: 400.0)))
66+
expect(scrollView.contentOffset).to(equal(CGPoint(x: 30, y: 30)))
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)