138

How to remove the left and right Padding of a List in SwiftUI? Every List i create has borders to the leading and trailing of a cell.

What modifier should I add to remove this?

Just a coder
  • 15,480
  • 16
  • 85
  • 138

10 Answers10

227

It looks like .listRowInsets doesn't work for rows in a List that is initialised with content.

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())
    }
}
Avario
  • 4,655
  • 3
  • 26
  • 19
  • 1
    ForEach with an array of Strings gives me the following error. "Cannot convert value of type '[String]' to expected argument type 'Range'" – a.ajwani Apr 10 '20 at 23:52
  • I also find applying the .listRowInsets(_) modifier to a Section within a List achieves the desired outcome. – Neil Smith Aug 04 '20 at 13:36
  • 3
    Much more preferable is the answer listed below `.listStyle(GroupedListStyle())` which causes all the Section and Rows to indent neatly. – Chris Kemp Nov 26 '20 at 10:01
  • Not sure what's the logic behind, but it works for me! – taffykula Jul 27 '21 at 06:49
  • 2
    Worth noting that there is also a default minimum height for list items. If your views are smaller than this amount, you will see apparent insets that persist despite these changes. Consider: `.environment( \.defaultMinListRowHeight, 0 )` on the `List`. – lhunath Sep 15 '21 at 22:09
  • Separators don't match left indentation of content using this approach. They're still where they were before. – Casey Perkins Mar 11 '22 at 14:22
  • worked me for tvOS – Abhishek Thapliyal May 05 '22 at 11:02
154

Seem we can use PlainListStyle for List for iOS 14

List {
    Text("Row")
}
.listStyle(PlainListStyle())
Tai Le
  • 8,530
  • 5
  • 41
  • 34
29

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. You must use the modifier on the view inside the List for the modifier to have an effect.

Example usage:

List {
    Text("test")
        .listRowInsets(EdgeInsets(top: -20, leading: -20, bottom: -20, trailing: -20))
}
gnarlybracket
  • 1,691
  • 4
  • 18
  • 37
  • Looks like you got it wrong. The documentation says that, the insets will be applied to the view inside the list. It doesn't specify that the modifier should be used on the view in the List. – Felix Marianayagam Feb 12 '20 at 11:45
  • 1
    @FelixMarianayagam Nothing is stopping you from using the modifier on the `List` itself, but it won't have any effect on the list's items. You need to apply the modifier to a view **inside a list** as stated by the documentation. I have updated my answer to be more clear. – gnarlybracket May 26 '20 at 17:15
19

The modifier was

.listRowInsets(EdgeInsets(......))
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
Just a coder
  • 15,480
  • 16
  • 85
  • 138
18

There are several spacings that you can change in a list. Take a look at this color-coded map and pick the one that fits your needs:

List(1...100, id: \.self) { item in
    Text("\(item)")
        .padding() //  comment to remove PURPLE padding
        .background(Color.yellow)
        // .listRowInsets(EdgeInsets()) //  uncomment to remove BLUE inset
}
// .listStyle(.plain) //  uncomment to remove GREEN inset
// .listStyle(.grouped) //  uncomment to remove RED inset

Demo

RTL-Demo

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • 3
    Beautiifully written and illustrated answer! :) It also took me a sec to realize why your list's item was over on the right edge when I didn't see anything in the markup. Then it hit me... I'm guessing you're in a country that uses right-to-left reading direction instead of left-to-right like me! That's what I love about Stackoverflow... you can learn something new while learning something else new. Again, great answer! :) – Mark A. Donohoe May 24 '23 at 18:47
  • 1
    Oh I totally forgot about the direction since RTL is my primary and I didn't even notice! I've changed the default. Thanks for pointing that out – Mojtaba Hosseini May 25 '23 at 11:10
  • You didn't have to change it! Be proud of your locality! All that's needed is to add a note saying 'In a RTL locale such as mine, this is how it would appear.' kinda thing. – Mark A. Donohoe May 25 '23 at 17:18
12

You should call

.listRowInsets()

for row, like this:

List(items) {
            YoursRowView()
                .listRowInsets(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
        }
Rustam Khisamov
  • 151
  • 1
  • 4
  • 8
10
.listStyle(GroupedListStyle())
Tatyana
  • 129
  • 1
  • 2
  • 1
    Why do you recommend this over the `.listRowInsets()` approach proposed by previous answers? Does it provide any advantages over those previous approaches? – Jeremy Caney May 13 '20 at 05:43
  • 2
    Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL May 13 '20 at 08:55
  • 1
    Although the quality of the answer is poor, this is the correct answer. It expands the Sections and Rows to the full width. – Chris Kemp Nov 26 '20 at 10:00
  • 1
    holyyyy smokes! I spend over 4 hours figuring this out and this line of code solved it all! u rock – Coffee and Waffles Nov 30 '21 at 13:53
  • look at this answer for better information. https://stackoverflow.com/a/75722515/14180967 – Shivam Garg Mar 13 '23 at 13:30
10

Remove paddings

iOS 15, Xcode 13

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.

Note that this is a completely different 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.

atereshkov
  • 4,311
  • 1
  • 38
  • 49
  • 2
    This is the only solution that works for me in iOS14.2. I tried the `.listStyle(...)` and `.listRowInsets(...)` suggestions in all possible locations in simple examples and neither removes the leading space – Jason Campbell Feb 11 '21 at 12:31
  • this destroys the scroll position if the list mutates – Alex Mar 11 '21 at 03:06
  • 5
    Note that this is a completely different 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. – koleS Apr 13 '21 at 14:29
  • 1
    Another thing to be aware of is that if you use a `ScrollView` instead of a `List` then you can't easily use the swipe to delete and edit mode functionality that the list provides. You can of course make your own but that's more work. – Upholder Of Truth Feb 19 '22 at 09:39
6

2022, MacOS

in case of other solutions will not work (and they at the moment does not [for macOS]), you can use the following hack for macOS:

List(selection: $selectedItems) {
    // some content
}
// solution
.padding(EdgeInsets(top: -10, leading: -20, bottom: -10, trailing: -20))
.clipShape(Rectangle())
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
5

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.

aheze
  • 24,434
  • 8
  • 68
  • 125
Duck
  • 34,902
  • 47
  • 248
  • 470