【问题标题】:SwiftUI - Remove space between cellsSwiftUI - 删除单元格之间的空间
【发布时间】:2019-12-03 18:12:35
【问题描述】:

我正在 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)
        }
    }
}

我试过这个修饰符:

.padding(EdgeInsets(top: 0, leading: -20, bottom: 20, trailing: -20))

但是我对这种方法有两个问题:首先,我认为写字面负值不方便。其次,底部填充对任何值都不起作用。

那么有什么建议吗?

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    我在 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))
            }
        }
    }
    

    【讨论】:

    • 您也可以一次将其应用于整个列表:List { ... }.listRowInsets(EdgeInsets())
    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2012-01-18
    • 2020-03-25
    • 2014-10-01
    相关资源
    最近更新 更多