187

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?

13 Answers 13

275

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())
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

ForEach with an array of Strings gives me the following error. "Cannot convert value of type '[String]' to expected argument type 'Range<Int>'"
I also find applying the .listRowInsets(_) modifier to a Section within a List achieves the desired outcome.
Much more preferable is the answer listed below .listStyle(GroupedListStyle()) which causes all the Section and Rows to indent neatly.
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.
Separators don't match left indentation of content using this approach. They're still where they were before.
|
194

Seem we can use PlainListStyle for List for iOS 14

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

1 Comment

iOS 17: .listStyle(.plain)
145

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 all GREEN insets all together
// .contentMargins(<#edge#>, 0, for: .scrollContent) // 🟢 uncomment to customize combination of GREEN insets. (Requires iOS 17)
// .listStyle(.grouped) // 🔴 uncomment to remove RED inset

Demo

RTL-Demo Image

For Section:

You can use these extra modifiers as well:

.listSectionSpacing(0) // Space between sections
.listSectionMargins(.horizontal, 0) // Space around sections. (Requires iOS 26)

3 Comments

Great answer! This one explains everything and provides the necessary visual references to the different types of padding that one would want to remove. ✨
Any idea how to remove ONLY the top green part but not the leading/trailing green parts?
@RobertBasamac it is now possible from iOS 17. Added as a new 🟢 comment to the answer. For your case, you can pass .top as the edge.
31

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

Comments

21

The modifier was

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

Comments

14

You should call

.listRowInsets()

for row, like this:

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

Comments

13

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

also you can do extension with style override of base component of List. This will be sth like:

extension NSTableView {
    open override func viewDidMoveToWindow() {
        super.viewDidMoveToWindow()
    
        // code to remove paddings
        // or change another style like remove bg or sth like this
    }
}

2 Comments

not a good option in 13.4 this needs to be -8 and offsets the scroll bar also :(
@PeterLapisu can any other options solve this issue?
11
.listStyle(GroupedListStyle())

2 Comments

Although this code might solve the problem, a good answer should also explain what the code does and how it helps.
look at this answer for better information. stackoverflow.com/a/75722515/14180967
8

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.

3 Comments

this destroys the scroll position if the list mutates
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.
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.
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.

1 Comment

When i select an item, it selects all the rows instead using this code.
2

What worked for me is

import SwiftUI

struct DefaultListModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .listStyle(.plain)
            .scrollContentBackground(.hidden)
            .background(Color.clear)
    }
}

struct DefaultListItemModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .listRowBackground(Color.clear)
            .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
            .listRowSeparator(.hidden, edges: [.bottom])
    }
}

Example usage:

    List {
        if vm.followers.users.isEmpty {
            AppText("No followers yet.", type: .navMedium)
                .modifier(DefaultListItemModifier())

        } else {
            ForEach(vm.followers.users) { player in
                PlayerSocialItemView(player: player)
                .modifier(DefaultListItemModifier())
                .padding(.horizontal, smallerHorizontalPadding)
                .onAppear {
                    if vm.followers.users.last == player {
                        vm.fetchFollowers()
                    }
                }
            }

        }
    }
    .modifier(DefaultListModifier())
    .refreshable {
        vm.resetPagination()
        vm.fetchFollowers()
    }

Comments

-1

Trailing is big, you can use this: .listRowInsets(EdgeInsets(top: 8, leading: 0, bottom: 8, trailing: 0)) .listRowBackground(Color.color_background)

So the white inset desapear.

Comments

-1

In my case it was a combination of both modifiers:

.listRowInsets(EdgeInsets()) applied on the main list content and .listStyle(PlainListStyle()) applied on the list view itself:

List{
                Section {
                    VStack(spacing:16) {
                        if isLoading {
                            ForEach(0..<5,id:\.self) { _ in
                                ProjectItemSkeleton()
                            }
                        }
                        else{
                            ForEach(store.projects,id:\._id) { projectItem in
                                NavigationLink(value: projectItem) {
                                    SProjectItem(project: projectItem)
                                        .foregroundStyle(Color.theme.foreground)
                                }
                                .simultaneousGesture(TapGesture().onEnded({ _ in
                                    store.send(.setCurrentProjectSelected(projectItem.name))
                                }))
                            }
                        }
                    }
                    
                } header: {
                    VStack(spacing:16) {
                        HStack {
                            Text("Your")
                            Text("Projects")
                                .fontWeight(.bold)
                            Text("Are Here!")
                            
                        }
                        .font(.title)
                        .frame(maxWidth: .infinity,alignment: .leading)
                        .padding(.horizontal,12)
                        .padding(.vertical,0)
                        
                        HStack {
                            SSearchField(searchValue: $searchText)
                            
                            Button {
                                
                            } label: {
                                Image(systemName: "slider.horizontal.3")
                                    .foregroundStyle(.white)
                                    .fontWeight(.medium)
                                    .font(.system(size: 24))
                                    .frame(width:50.66,height: 50.66)
                                    .background {
                                        Circle().fill(Color.theme.primary)
                                    }
                            }
                        }
                    }
                    .padding(.bottom,16)
                    .background(content: {
                        Color.white
                    })
                }
                .listRowInsets(EdgeInsets())
            }
            .listStyle(PlainListStyle())
            .listSectionSeparator(.hidden)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.