2

当另一个视图被定义后,我试图删除列表视图的底部填充,以便列表视图位于另一个视图之上。不叠加。似乎从列表中创建了一些额外的空间,我不确定如何删除它以实现我想要的。这就是我所拥有的。

List {
    ForEach(0...2, id: \.self) { color in
        Text("\(color)")
    }.listRowInsets(EdgeInsets())
}

Rectangle().fill(Color.red)

这就是它的样子。您可以看到第 2 行和红色矩形之间有一个间隙。我想要的是这两个视图的边界彼此相邻。

布局

编辑:我尝试将代码嵌入到 VStack 中,但这并没有消除我们在下面看到的白色间隙。

看看这个答案,似乎将列表包装在 VStack 中会消除差距,但这不是我们在这里看到的?

VStack(spacing: 0) {
    VStack(spacing: 0) {
        List {
            ForEach(0...0, id: \.self) { number in
                Text("1")
                    .listRowBackground(Color.blue)
                Text("2")
            }.listRowBackground(Color.gray)
        }
    }

    VStack(spacing: 0) {
        HStack(spacing: 0) {
            Rectangle()
                .fill(Color.red)
            Rectangle()
                .fill(Color.red)
        }
        HStack(spacing: 0) {
            Rectangle()
                .fill(Color.red)
            Rectangle()
                .fill(Color.red)
        }
    }
    .aspectRatio(1.0, contentMode: .fill)

    VStack {
        List {
            Text("1")
                .listRowBackground(Color.purple)
            Text("2")
                .listRowBackground(Color.pink)
            Text("3")
                .listRowBackground(Color.blue)
            Text("4")
                .listRowBackground(Color.orange)
        }
    }
}

在此处输入图像描述

我已经尝试了多种间距等于 0 的 VStack 变体,但还没有找到如何消除由 List 视图创建的间隙。如果添加了更多行,它会开始填满,但我试图在红色矩形上方只放置两行没有白色间隙的行。

我们可以看到下面突出显示的列表视图包含该差距。

在此处输入图像描述

4

0 回答 0