iosswiftswiftui

SwiftUI List shows separator under last row — why?


SwiftUI List shows separator under last row — why? Normally, separators exist only between rows.

List {
    ForEach(displayData) { item in
        CustomRow(
            item: item,
            onTap: handleItemTap
        )
        .listRowBackground(Color.clear)
    }
}
.listStyle(.plain)
.background(
    RoundedRectangle(cornerRadius: 10)
        .fill(Color(UIColor.secondarySystemBackground))
)
.padding()

Solution

  • You can use .listRowSeparator to hide the separators selectively:

    List {
        ForEach(1..<11) { i in
            Text("Row \(i)")
                .listRowBackground(Color.clear)
                .listRowSeparator(i == 10 ? .hidden : .visible, edges: .bottom) // 👈 here
        }
    }
    .listStyle(.plain)
    // + background and padding, as before
    

    Screenshot


    In this particular case, you could also consider using .insetGrouped as list style instead:

    var body: some View {
        List {
            ForEach(1..<11) { i in
                Text("Row \(i)")
            }
        }
        .listStyle(.insetGrouped)
    }
    

    Screenshot