How can I set the spacing between sections of a list? I want 5 for first, 10 for second, 15 for third.

Asked on 2024-07-30

1 search

To set the spacing between sections of a list in SwiftUI, you can use the Section view along with custom spacing. Here's an example of how you can achieve different spacings for each section:

import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            Section(header: Text("First Section")) {
                Text("Item 1")
                Text("Item 2")
            }
            .padding(.bottom, 5) // 5 points spacing after the first section

            Section(header: Text("Second Section")) {
                Text("Item 1")
                Text("Item 2")
            }
            .padding(.bottom, 10) // 10 points spacing after the second section

            Section(header: Text("Third Section")) {
                Text("Item 1")
                Text("Item 2")
            }
            .padding(.bottom, 15) // 15 points spacing after the third section
        }
    }
}

In this example, each Section is followed by a .padding(.bottom, value) modifier to set the spacing after the section. Adjust the value to set the desired spacing for each section.

For more detailed information on working with sections in SwiftUI, you can refer to the session Demystify SwiftUI containers from WWDC 2024.