How to create spacing between items in a SwiftUI List view?

13,035

Solution 1

You can define the minimum list row height to be bigger so you'll have more separation between rows.

List (ratesVM.ratesList, id: \.self) { rate in
       Image(systemName: "plus")
       Button(action: {print("pressed")}) {
                    Text(rate.hebrewCurrencyName)
       }
}.environment(\.defaultMinListRowHeight, 50) //minimum row height

Alternatively you can build your row as a HStack and specify a frame height.

List (ratesVM.ratesList, id: \.self) { rate in
    HStack {
       Image(systemName: "plus")
       Button(action: {print("pressed")}) {
          Text(rate.hebrewCurrencyName)
       }
    }.frame(height: 50) //your row height 
}.environment(\.defaultMinListRowHeight, 20) 

Or as a VStack and and use Spacers

List (ratesVM.ratesList, id: \.self) { rate in
    VStack{
        Spacer()
        HStack {
           Image(systemName: "plus")
           Button(action: {print("pressed")}) {
              Text(rate.hebrewCurrencyName)
           }
        }
        Spacer()
    }.frame(height: 50)
}.environment(\.defaultMinListRowHeight, 20) 

Solution 2

SwiftUI lets us set individual padding around views using the padding() modifier. If you use this with no parameters you’ll get system-default padding on all sides, like this:

VStack {
    Text("SwiftUI")
        .padding()
    Text("rocks")
}

But you can also customize how much padding to apply and where. So, you might want to apply system padding to only one side:

Text("SwiftUI")
    .padding(.bottom)

Or you might want to control how much padding is applied to all sides:

Text("SwiftUI")
    .padding(100)

Or you can combine the two to add a specific amount of padding to one side of the view:

Text("SwiftUI")
    .padding(.bottom, 100)

so you can do

Text(rate.hebrewCurrencyName)
      .padding(50)

Solution 3

A quick solution here is to just set the corresponding listStyle to SidebarListStyle. For instance:

List {
    Text("First row")
    Text("Second row")
    Text("Third row")
}
.listStyle(SidebarListStyle())

But please note that on macOS and iOS, the sidebar list style also displays disclosure indicators in the section headers that allow the user to collapse and expand sections.

Another quick alternative would be to use the Divider() helper view instead (i.e., a visual element that can be used to separate other content):

List {
    Text("First row")
    Divider()
    Text("Second row")
    Divider()
    Text("Third row")
}

Both solutions won't allow direct control of the vertical spacing but at least provides a more spacious layout alternative.

Share:
13,035

Related videos on Youtube

Kenny Kurochkin
Author by

Kenny Kurochkin

Updated on June 04, 2022

Comments

  • Kenny Kurochkin
    Kenny Kurochkin almost 2 years

    I can't seem to find a way to create spacing between List items. Maybe I shouldn't put in a list in the first place?

    enter image description here

    What do you think?

    This the code that generates the view:

    struct ContentView: View {
        
        @ObservedObject var ratesVM = RatesViewModel()
        
        init() {
            UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            UITableView.appearance().backgroundColor = UIColor.init(named: "midnight blue")
            UITableViewCell.appearance().backgroundColor = .green
            UITableView.appearance().separatorStyle = .none
        }
        
        var body: some View {
            
            NavigationView {
                List (ratesVM.ratesList, id: \.self) { rate in
                        Image(systemName: "plus")
                        
                        Button(action: {print("pressed")}) {
                            Text(rate.hebrewCurrencyName)
                    }
                }
                .navigationBarTitle("המרות מטבע")
                .navigationBarItems(leading:
                    Button(action: {
                        print("button pressed")
                        self.ratesVM.callService()
                    }) {
                        Image(systemName: "plus")
                            .foregroundColor(Color.orange)})
            }.environment(\.layoutDirection, .rightToLeft)
        }
    }
    
  • Cristi Băluță
    Cristi Băluță almost 4 years
    Not working if you want to remove spacing. This only sets a minimum height of the cell but does not adjust the space between them.