Skip to content

Commit b6bd4e0

Browse files
committed
[fix] #180 PokitFlowLayout 잘못된 레이아웃 수정
1 parent bc9fb13 commit b6bd4e0

1 file changed

Lines changed: 47 additions & 27 deletions

File tree

Projects/DSKit/Sources/Components/PokitFlowLayout.swift

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,64 @@ public struct PokitFlowLayout: Layout {
2020
}
2121

2222
public func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
23-
var width: CGFloat = 0
24-
var height: CGFloat = 0
23+
var totalWidth: CGFloat = 0
24+
var totalHeight: CGFloat = 0
2525
var rowWidth: CGFloat = 0
2626
var rowHeight: CGFloat = 0
27-
28-
for subview in subviews {
29-
let size = subview.sizeThatFits(ProposedViewSize(width: proposal.width, height: nil))
30-
if rowWidth + size.width > proposal.width ?? .infinity {
31-
height += rowHeight + rowSpacing
32-
width = max(width, rowWidth)
33-
rowWidth = 0
34-
rowHeight = 0
27+
let maxWidth = proposal.width ?? CGFloat.infinity
28+
29+
for (_, subview) in subviews.enumerated() {
30+
let subviewSize = subview.sizeThatFits(ProposedViewSize(width: maxWidth, height: nil))
31+
let itemWidth = subviewSize.width
32+
let itemHeight = subviewSize.height
33+
34+
if rowWidth > 0 && (rowWidth + colSpacing + itemWidth) > maxWidth {
35+
// 현재 행 마무리
36+
totalWidth = max(totalWidth, rowWidth)
37+
totalHeight += rowHeight + rowSpacing
38+
// 새로운 행 시작
39+
rowWidth = itemWidth
40+
rowHeight = itemHeight
41+
} else {
42+
if rowWidth > 0 {
43+
rowWidth += colSpacing
44+
}
45+
rowWidth += itemWidth
46+
rowHeight = max(rowHeight, itemHeight)
3547
}
36-
rowWidth += size.width + colSpacing
37-
rowHeight = max(rowHeight, size.height)
3848
}
39-
40-
height += rowHeight
41-
width = max(width, rowWidth)
42-
43-
return CGSize(width: width, height: height)
49+
50+
// 마지막 행 높이 추가
51+
totalWidth = max(totalWidth, rowWidth)
52+
totalHeight += rowHeight + rowSpacing
53+
54+
return CGSize(width: totalWidth, height: totalHeight)
4455
}
45-
56+
4657
public func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
47-
var x: CGFloat = bounds.minX
48-
var y: CGFloat = bounds.minY
58+
var x = bounds.minX
59+
var y = bounds.minY
4960
var rowHeight: CGFloat = 0
50-
51-
for subview in subviews {
52-
let size = subview.sizeThatFits(ProposedViewSize(width: bounds.width, height: nil))
53-
if x + size.width > bounds.width {
61+
let maxX = bounds.maxX
62+
63+
for (_, subview) in subviews.enumerated() {
64+
let subviewSize = subview.sizeThatFits(ProposedViewSize(width: bounds.width, height: nil))
65+
let itemWidth = subviewSize.width
66+
let itemHeight = subviewSize.height
67+
68+
if x > bounds.minX - 1 && (x + colSpacing + itemWidth) > maxX + 1 {
69+
// 현재 행 마무리하고 다음 행 시작
5470
x = bounds.minX
5571
y += rowHeight + rowSpacing
5672
rowHeight = 0
5773
}
58-
subview.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(size))
59-
x += size.width + colSpacing
60-
rowHeight = max(rowHeight, size.height)
74+
if x > bounds.minX {
75+
x += colSpacing
76+
}
77+
// 아이템 배치
78+
subview.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(subviewSize))
79+
x += itemWidth
80+
rowHeight = max(rowHeight, itemHeight)
6181
}
6282
}
6383
}

0 commit comments

Comments
 (0)