Skip to content

Commit d84b2a4

Browse files
author
Luc Dion
committed
Update following reviews + update doc
1 parent 174137d commit d84b2a4

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Extremely Fast views layouting without auto layout. No magic, pure code, full co
3737
* Swift 3.2+ / Swift 4.0 / Objective-C
3838

3939
### Recent features
40-
* :star: Add `wrapContent()` methods that adjust view's width & height to wrap all its subviews. See [wrapContent](#wrapContent) for more information.
40+
* :star: Add `wrapContent()` methods that adjust view's width and height to wrap all its subviews. See [wrapContent](#wrapContent) for more information.
4141

4242
* :star: PinLayout now support macOS. See [macOS Support](#macos_support) for more information.
4343

@@ -1147,10 +1147,17 @@ The following methods are useful to adjust view's width and/or height to wrap al
11471147
* **`wrapContent()`**
11481148
**`wrapContent(padding: CGFloat)`**
11491149
**`wrapContent(padding: UIEdgeInsets)`**
1150-
Adjust the view's width and height to wrap all its subviews. The method also adjusts subviews position to create a tight wrap. It is also possible to specify an optional padding around all subviews.
1150+
Adjust the view's width and height to wrap all its subviews. The method also adjusts subviews's position to create a tight wrap. It is also possible to specify an optional padding around all subviews.
11511151
* **`wrapContent(:WrapType)`**
1152-
**`wrapContent(:WrapType, padding: CGFloat)`** **`wrapContent(:WrapType, padding: UIEdgeInsets)`**
1153-
Adjust the view's width AND/OR height to wrap all its subviews. WrapType values are `.horizontally`/`.vertically`/`.all` It is also possible to specify an optional padding around all subviews.
1152+
**`wrapContent(:WrapType, padding: CGFloat)`** **`wrapContent(:WrapType, padding: UIEdgeInsets)`**
1153+
Adjust the view's width AND/OR height to wrap all its subviews. Accept a WrapType parameter to define the wrapping type. It is also possible to specify an optional padding around all subviews.
1154+
1155+
**Types:**
1156+
1157+
* **`WrapType`** values:
1158+
* `.horizontally`: Adjust the view's width and update subviews's horizontal position.
1159+
* `.vertically`: Adjust only the view's height and update subviews's vertical position.
1160+
* `.all`: Adjust the view's width AND height and update subviews position. This is the default WrapType parameter value `wrapContent()` methods.
11541161

11551162
###### Usage examples:
11561163
```swift

Sources/Impl/PinLayoutImpl+WrapContent.swift

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,31 @@ extension PinLayoutImpl {
5151
private func wrapContent(_ type: WrapType, padding: PEdgeInsets, _ context: Context) -> PinLayout {
5252
let subviews = view.subviews
5353
guard !subviews.isEmpty else { return self }
54-
let firstViewFrame = subviews[0].frame
55-
var minX = firstViewFrame.minX
56-
var maxX = firstViewFrame.maxX
57-
var minY = firstViewFrame.minY
58-
var maxY = firstViewFrame.maxY
5954

60-
for i in 1..<subviews.count {
61-
let frame = subviews[i].frame
62-
if frame.minX < minX {
63-
minX = frame.minX
64-
}
65-
if frame.maxX > maxX {
66-
maxX = frame.maxX
67-
}
68-
if frame.minY < minY {
69-
minY = frame.minY
70-
}
71-
if frame.maxY > maxY {
72-
maxY = frame.maxY
73-
}
74-
}
55+
let firstViewRect = Coordinates.getViewRect(subviews[0], keepTransform: keepTransform)
56+
let boundingRect = subviews.reduce(firstViewRect, { (result, view) in
57+
result.union(Coordinates.getViewRect(view, keepTransform: keepTransform))
58+
})
7559

7660
var offsetDx: CGFloat = 0
7761
var offsetDy: CGFloat = 0
7862

7963
if type == .all || type == .horizontally {
80-
let contentWidth = maxX - minX + padding.left + padding.right
64+
let contentWidth = boundingRect.width + padding.left + padding.right
8165
if contentWidth >= 0 {
8266
setWidth(contentWidth, context)
8367
}
8468

85-
offsetDx = -minX + padding.left
69+
offsetDx = -boundingRect.minX + padding.left
8670
}
8771

8872
if type == .all || type == .vertically {
89-
let contentHeight = maxY - minY + padding.top + padding.bottom
73+
let contentHeight = boundingRect.height + padding.top + padding.bottom
9074
if contentHeight >= 0 {
9175
setHeight(contentHeight, context)
9276
}
9377

94-
offsetDy = -minY + padding.top
78+
offsetDy = -boundingRect.minY + padding.top
9579
}
9680

9781
if offsetDx != 0 || offsetDy != 0 {

0 commit comments

Comments
 (0)