import SwiftUI /// 纯计算的行式布局:给定子视图宽度与容器宽度,算出每行起点与总高度。 public struct FlowEngine { public let spacing: CGFloat public init(spacing: CGFloat = 8) { self.spacing = spacing } public struct Placement: Equatable, Sendable { public let x: CGFloat public let y: CGFloat } public struct Result: Equatable, Sendable { public let placements: [Placement] public let size: CGSize public let rows: Int } public func layout(widths: [CGFloat], heights: [CGFloat], maxWidth: CGFloat) -> Result { var placements: [Placement] = [] var x: CGFloat = 0 var y: CGFloat = 0 var rowHeight: CGFloat = 0 var widest: CGFloat = 0 var rows = 1 for (index, width) in widths.enumerated() { let needsBreak = x > 0 && x + width > maxWidth if needsBreak { y += rowHeight + spacing x = 0 rowHeight = 0 rows += 1 } placements.append(Placement(x: x, y: y)) x += width + spacing rowHeight = max(rowHeight, heights[index]) widest = max(widest, x - spacing) } let height = rows == 1 ? rowHeight : y + rowHeight return Result( placements: placements, size: CGSize(width: min(widest, maxWidth), height: height), rows: rows ) } } /// 把 `FlowEngine` 接进 SwiftUI 的 `Layout` 协议。 public struct FlowLayout: Layout { public var spacing: CGFloat public init(spacing: CGFloat = 8) { self.spacing = spacing } public func sizeThatFits( proposal: ProposedViewSize, subviews: Subviews, cache: inout Void ) -> CGSize { let sizes = subviews.map { $0.sizeThatFits(.unspecified) } let maxWidth = proposal.width ?? .infinity return FlowEngine(spacing: spacing) .layout( widths: sizes.map(\.width), heights: sizes.map(\.height), maxWidth: maxWidth ) .size } public func placeSubviews( in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Void ) { let sizes = subviews.map { $0.sizeThatFits(.unspecified) } let result = FlowEngine(spacing: spacing).layout( widths: sizes.map(\.width), heights: sizes.map(\.height), maxWidth: bounds.width ) for (index, subview) in subviews.enumerated() { let point = result.placements[index] subview.place( at: CGPoint(x: bounds.minX + point.x, y: bounds.minY + point.y), anchor: .topLeading, proposal: ProposedViewSize(sizes[index]) ) } } } /// 五个固定尺寸色块,用来观察换行。 public struct FlowItems: View { public init() {} public var body: some View { FlowLayout(spacing: 8) { ForEach(0..<5, id: \.self) { index in Rectangle() .fill(index.isMultiple(of: 2) ? Color.blue : Color.orange) .frame(width: 40, height: 20) } } } } /// 两个分支:第一个放不下时选第二个。 public struct FitChoice: View { public init() {} public var body: some View { ViewThatFits(in: .horizontal) { Rectangle().fill(.red).frame(width: 300, height: 20) Rectangle().fill(.blue).frame(width: 100, height: 20) } } } /// 同一份内容用 `AnyLayout` 在横排与竖排之间切换。 public struct LayoutSwitch: View { public let horizontal: Bool public init(horizontal: Bool) { self.horizontal = horizontal } public var body: some View { let layout = horizontal ? AnyLayout(HStackLayout()) : AnyLayout(VStackLayout()) layout { Rectangle().fill(.blue).frame(width: 20, height: 20) Rectangle().fill(.red).frame(width: 20, height: 20) } } } /// 两列网格,用固定尺寸矩形避免字体差异。 public struct SimpleGrid: View { public init() {} public var body: some View { Grid(horizontalSpacing: 0, verticalSpacing: 0) { GridRow { Rectangle().fill(.blue).frame(width: 30, height: 10) Rectangle().fill(.red).frame(width: 50, height: 10) } GridRow { Rectangle().fill(.green).frame(width: 30, height: 10) Rectangle().fill(.orange).frame(width: 50, height: 10) } } } } /// 截图:流式布局与三个进阶布局并排。 public struct AdvancedBoard: View { public init() {} public var body: some View { VStack(alignment: .leading, spacing: 16) { section("FlowLayout(宽 100)") { FlowItems().frame(width: 100, alignment: .leading).border(.quaternary) } section("ViewThatFits(宽 200)") { FitChoice().frame(width: 200, alignment: .leading).border(.quaternary) } section("AnyLayout 与 Grid") { HStack(alignment: .top, spacing: 24) { LayoutSwitch(horizontal: true).border(.quaternary) LayoutSwitch(horizontal: false).border(.quaternary) SimpleGrid().border(.quaternary) } } } .padding(16) .background(Color.white) } private func section(_ title: String, @ViewBuilder content: () -> some View) -> some View { VStack(alignment: .leading, spacing: 6) { Text(title).font(.system(size: 11, design: .monospaced)).foregroundStyle(.secondary) content() } } }