SwiftUI - 删除单元格之间的空间 [英] SwiftUI - Remove space between cells

查看:29
本文介绍了SwiftUI - 删除单元格之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 SwiftUI 中实现列表视图.我要尝试的是让单元格在任何其他单元格或父视图之间没有空间.

I am implementing a list view in SwiftUI. What I am trying to approach is to have cells that has no space between any other cells or parent view.

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

So in this screenshot as you can see there is a space between every cell and also space with the edge of the phone, which I want to remove.

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))

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

But I have two problems with this approach: First, I don't think its convenient to write literal negative values. Second, the bottom padding does not work with any value.

所以有什么建议吗?

推荐答案

listRowInsets 给我带来了好运

I've had good luck with 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))
        }
    }
}

这篇关于SwiftUI - 删除单元格之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆