SwiftUI-删除单元格之间的空间

Mos*_*fat 6 swift swiftui

我在SwiftUI中实现列表视图。我试图解决的问题是使任何其他单元格或父视图之间没有空间的单元格。

在此处输入图片说明

因此,在此屏幕截图中,您可以看到每个单元格之间都有一个空间,手机边缘也有空间,我想删除该空间。

struct FlickrView : View {
    var flickrResponse: [FlickrResponse]
    var body: some View {
        List(flickrResponse) { item in
            FlickrImageCell(response: item)
        }
    }
}

struct FlickrImageCell : View {
    var response: FlickrResponse
    var body: some View {
        return ZStack(alignment: .topLeading) {
            Image(uiImage: response.image ?? UIImage())
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: nil, height: 100.0, alignment: .center)
                .clipShape(Rectangle())
                .padding(0)
            Text(response.title).fontWeight(.medium).multilineTextAlignment(.center)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经试过这个修饰符:

.padding(EdgeInsets(top: 0, leading: -20, bottom: 20, trailing: -20))
Run Code Online (Sandbox Code Playgroud)

但是这种方法有两个问题:首先,我认为编写文字负值并不方便。其次,底部填充不能使用任何值。

有什么建议吗?

MSc*_*ler 21

我在 listRowInsets 上很幸运

struct ContentView: View {
    var body: some View {
        List {
            Color.red
            .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
            Color.blue
            .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
            Color.yellow
            .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 顶部和底部修饰符不会改变我的行距 (11认同)
  • 您还可以立即将其应用于整个列表:`List { ... }.listRowInsets(EdgeInsets())` (2认同)