如何让 GridItem 使用 LazyVGrid 中所有可用的 space?

How to make a GridItem use all of the available space in a LazyVGrid?

我希望我的 LazyVGrid 的第 2 列填充所有可用的 space(有点像 css 中的 flex:1)。我应该可以使用 .flexible() 来完成它,但我无法让它工作

我错过了什么?预先感谢您的帮助!

我的代码:

LazyVGrid(columns: [
    GridItem(),
    GridItem(.flexible()),
]) {
    ProfilePicture()
    ProfilePicture()
    ProfilePicture()
    ProfilePicture()
}

截图:

expected

what I get instead

n.b.

您没有给出用例或 Minimal Reproducible Example (MRE),因此除了第二张图片之外,很难确定您想要什么。你不能用 GridItem.Size 产生那个。您可以通过在 LazyVGrid 上设置 alignment 来实现这种效果,如下所示:

struct GridItemAvailSpace: View {
    let columns = [
        GridItem(),
        GridItem()
    ]
    var body: some View {
        LazyVGrid(columns: columns, alignment: .leading) {
            ProfilePicture()
            ProfilePicture()
            ProfilePicture()
            ProfilePicture()
        }
    }
}