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?
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())
}
}
.listStyle(GroupedListStyle()) which causes all the Section and Rows to indent neatly..environment( \.defaultMinListRowHeight, 0 ) on the List.Seem we can use PlainListStyle for List for iOS 14
List {
Text("Row")
}
.listStyle(PlainListStyle())
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

Section:You can use these extra modifiers as well:
.listSectionSpacing(0) // Space between sections
.listSectionMargins(.horizontal, 0) // Space around sections. (Requires iOS 26)
.top as the edge.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))
}
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
}
}
.listStyle(GroupedListStyle())
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.
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.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.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.
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()
}
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)