JustifiableFlowLayout

一个 SwiftUI 布局,它按从左到右、从上到下的顺序排列一系列项目。

默认情况下,它是一个标准的流式布局,项目之间应用精确的间距。例如:

它也可以用作“对齐”的流式布局,其中最宽的项目决定间距和对齐方式,从而创建整齐列的均匀外观。例如:

支持动画。这是一个同时添加和删除项目的示例:

Animation showing the addition and removal of items simultaneously

用法

示例

import JustifiableFlowLayout

let uniqueWords = ["cloud", "hill", "thunder", "if", "run", "fright", "just", "twenty", "agent", "theatre", "ink", "so", "rig", "week", "range", "today"]

JustifiableFlowLayout(minSpacing: 10, shouldJustify: false) {
    ForEach(uniqueWords, id: \.self) { word in
        Text(word)
            .padding(10)
            .border(Color.mint, width: 2)
    }
}

JustifiableFlowLayout 也可以很好地在 ScrollView 中使用。

安装

Swift Package Manager

将 Swift 包添加到您的项目中

https://github.com/lorin-vr/JustifiableFlowLayout.git

讨论

JustifiableFlowLayout 可以作为 SwiftUI 集合视图布局(如 GridLazyVGrid)的替代方案。

LazyVGrid 不同,JustifiableFlowLayout 一次性布局所有项目。这可能允许更高效的布局和动画,但可能不适合大型项目集合。

Grid 不同,您不需要指定所需的行数。 JustifiableFlowLayout 将根据项目大小决定需要多少行。使用 JustifiableFlowLayout 时,无需定义 GridItemGridRow