Skip to content

Commit e0313f5

Browse files
authored
Merge pull request #152 from layoutBox/fix_uiview_layout
Fix a regression
2 parents 66a1b50 + 40721c9 commit e0313f5

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Sources/Extensions/CALayer+PinLayout.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ extension CALayer: Layoutable {
3030
public func getRect(keepTransform: Bool) -> CGRect {
3131
if keepTransform {
3232
/*
33-
To adjust the view's position and size, we don't set the UIView's frame directly, because we want to keep the
34-
view's transform (UIView.transform).
35-
By setting the view's center and bounds we really set the frame of the non-transformed view, and this keep
36-
the view's transform. So view's transforms won't be affected/altered by PinLayout.
33+
To adjust the layer's position and size, we don't set the layer's frame directly, because we want to keep the
34+
layer's transform.
35+
By setting the layer's center and bounds we really set the frame of the non-transformed layer, and this keep
36+
the layer's transform. So layer's transforms won't be affected/altered by PinLayout.
3737
*/
38-
3938
let size = bounds.size
4039
// See setRect(...) for details about this calculation.
4140
let origin = CGPoint(x: position.x - (size.width * anchorPoint.x),
@@ -52,10 +51,10 @@ extension CALayer: Layoutable {
5251

5352
if keepTransform {
5453
/*
55-
To adjust the view's position and size, we don't set the UIView's frame directly, because we want to keep the
56-
view's transform (UIView.transform).
57-
By setting the view's center and bounds we really set the frame of the non-transformed view, and this keep
58-
the view's transform. So view's transforms won't be affected/altered by PinLayout.
54+
To adjust the layer's position and size, we don't set the layer's frame directly, because we want to keep the
55+
layer's transform.
56+
By setting the layer's center and bounds we really set the frame of the non-transformed layer, and this keep
57+
the layer's transform. So layer's transforms won't be affected/altered by PinLayout.
5958
*/
6059

6160
// NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account.

Sources/Extensions/UIView+PinLayout.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,43 @@ extension UIView: Layoutable, SizeCalculable {
3838
}
3939

4040
public func getRect(keepTransform: Bool) -> CGRect {
41-
return layer.getRect(keepTransform: keepTransform)
41+
if keepTransform {
42+
/*
43+
To adjust the view's position and size, we don't set the UIView's frame directly, because we want to keep the
44+
view's transform (UIView.transform).
45+
By setting the view's center and bounds we really set the frame of the non-transformed view, and this keep
46+
the view's transform. So view's transforms won't be affected/altered by PinLayout.
47+
*/
48+
let size = bounds.size
49+
// See setRect(...) for details about this calculation.
50+
let origin = CGPoint(x: center.x - (size.width * layer.anchorPoint.x),
51+
y: center.y - (size.height * layer.anchorPoint.y))
52+
53+
return CGRect(origin: origin, size: size)
54+
} else {
55+
return frame
56+
}
4257
}
4358

4459
public func setRect(_ rect: CGRect, keepTransform: Bool) {
45-
layer.setRect(rect, keepTransform: keepTransform)
60+
let adjustedRect = Coordinates<View>.adjustRectToDisplayScale(rect)
61+
62+
if keepTransform {
63+
/*
64+
To adjust the view's position and size, we don't set the UIView's frame directly, because we want to keep the
65+
view's transform (UIView.transform).
66+
By setting the view's center and bounds we really set the frame of the non-transformed view, and this keep
67+
the view's transform. So view's transforms won't be affected/altered by PinLayout.
68+
*/
69+
70+
// NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account.
71+
center = CGPoint(x: adjustedRect.origin.x + (adjustedRect.width * layer.anchorPoint.x),
72+
y: adjustedRect.origin.y + (adjustedRect.height * layer.anchorPoint.y))
73+
// NOTE: We must set only the bounds's size and keep the origin.
74+
bounds.size = adjustedRect.size
75+
} else {
76+
frame = adjustedRect
77+
}
4678
}
4779

4880
public func isLTR() -> Bool {

0 commit comments

Comments
 (0)