简体   繁体   English

如何去除SwiftUI中列表的左右填充?

[英]How to remove the left and right Padding of a List in SwiftUI?

How to remove the left and right Padding of a List in SwiftUI?如何去除SwiftUI中列表的左右填充? Every List i create has borders to the leading and trailing of a cell.我创建的每个列表都有单元格的前导和尾随边界。

What modifier should I add to remove this?我应该添加什么修饰符来删除它?

It looks like .listRowInsets doesn't work for rows in a List that is initialised with content .看起来.listRowInsets不适用于用content初始化的List中的行。

So this doesn't work:所以这不起作用:

List(items) { item in
    ItemRow(item: item)
        .listRowInsets(EdgeInsets())
}

But this does:但这确实:

List {
    ForEach(items) { item in
        ItemRow(item: item)
            .listRowInsets(EdgeInsets())
    }
}

Seem we can use PlainListStyle for List for iOS 14似乎我们可以将PlainListStyle用于iOS 14 的List

List {
    Text("Row")
}
.listStyle(PlainListStyle())

修改器是

.listRowInsets(EdgeInsets(......))

Use this modifier:使用这个修饰符:

.listRowInsets(EdgeInsets(....))

However, as stated in the documentation , the insets will be applied to the view when inside a list .但是,如文档中所述,插入将应用于列表中的视图。

Sets the inset to be applied to the view when placed in a list.设置放置在列表中时要应用于视图的插图。 (emphasis mine) (强调我的)

Using this modifier on the List itself will have no effect on the views inside it.List本身上使用此修饰符不会影响其中的视图。 You must use the modifier on the view inside the List for the modifier to have an effect.您必须在List内的视图上使用修饰符才能使修饰符生效。

Example usage:用法示例:

List {
    Text("test")
        .listRowInsets(EdgeInsets(top: -20, leading: -20, bottom: -20, trailing: -20))
}

Remove paddings删除填充

iOS 14.2, Xcode 12.2 iOS 14.2,Xcode 12.2

ScrollView {
    LazyVStack {
        ForEach(viewModel.portfolios) { portfolio in
            PortfolioRow(item: portfolio)
        }
    }
}

This gives you complete control over the list (you can also remove separator using this code).这使您可以完全控制列表(您也可以使用此代码删除分隔符)。 Current implementation of List doesn't provide full control and contains some issues. List 的当前实现不提供完全控制并包含一些问题。

Note that this is a completely different API.请注意,这是一个完全不同的 API。 Both List and LazyVStack are lazy containers, but in contrast to List , LazyVStack doesn't reuse cells, which will SIGNIFICANTLY change the performance when rows are more complex views. ListLazyVStack都是惰性容器,但与ListLazyVStack不重用单元格,当行是更复杂的视图时,这将显着改变性能。

.listStyle(GroupedListStyle())

The above solutions did not solve for me because my List had sections.上述解决方案没有为我解决,因为我的列表有部分。

This solved for me:这为我解决了:

List {
    ForEach(years, id:\.self) { year in
        Section(header: SectionHeader(year)) {
            VStack(alignment:.leading) {
                ForEach(cars[year]!, id:\.self) { car in
                    ListItem(car)
                        .frame(width: UIScreen.main.bounds.size.width,
                               alignment: .center)
                        .listRowInsets(.init())
                }
            }
        }
        .frame(width: UIScreen.main.bounds.size.width,
               alignment: .center)
        .listRowInsets(.init())
        .listStyle(GroupedListStyle())
    }
}

In resume, you have to repeat the command for the section.在简历中,您必须为该部分重复该命令。

You should call你应该打电话

.listRowInsets() .listRowInsets()

for row, like this:对于行,像这样:

List(items) {
            YoursRowView()
                .listRowInsets(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
        }

2022, MacOS 2022 年,MacOS

List(selection: $selectedItems) {
    // some content
}
// solution
.padding(EdgeInsets(top: -10, leading: -20, bottom: -10, trailing: -20))
.clipShape(Rectangle())
import SwiftUI

struct ContentView: View {
    
    enum Flavor: String, CaseIterable, Identifiable {
        var id: String { self.rawValue }
        case sample1, sample2, sample3
    }
    var body: some View {
        VStack {
            GeometryReader { geometry in
                List {
                    ForEach(Flavor.allCases, id: \.self) { flavour in
                        Text(flavour.rawValue)
                            .frame(width: geometry.size.width,
                                   alignment: .leading)
                            .listRowInsets(.init())
                            .border(Color.red, width: 1)
                    }
                }
            }
        }
    }
}

It definitely works I tested Please like this:-)它绝对有效我测试过请像这样:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM