struct FlowLayout: Layout {
var spacing: CGFloat = 12
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
let maxWidth = proposal.width ?? .infinity
var rowWidth: CGFloat = 0
var rowHeight: CGFloat = 0
var totalHeight: CGFloat = 0
for subview in subviews {
let size = subview.sizeThatFits(.unspecified)
if rowWidth + size.width > maxWidth, rowWidth > 0 {
totalHeight += rowHeight + spacing
rowWidth = size.width + spacing
rowHeight = size.height
} else {
rowWidth += size.width + spacing
rowHeight = max(rowHeight, size.height)
}
}
totalHeight += rowHeight
return CGSize(width: maxWidth, height: totalHeight)
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
var x = bounds.minX
var y = bounds.minY
var rowHeight: CGFloat = 0
for subview in subviews {
let size = subview.sizeThatFits(.unspecified)
if x + size.width > bounds.maxX, x > bounds.minX {
x = bounds.minX
y += rowHeight + spacing
rowHeight = 0
}
subview.place(at: CGPoint(x: x, y: y),
anchor: .topLeading,
proposal: ProposedViewSize(size))
x += size.width + spacing
rowHeight = max(rowHeight, size.height)
}
}
}
Post #730
434
И код для кастомного Layout из видео
- ❤ 7