iosswiftswiftui

How can I remove the line separators from a List in SwiftUI without using ForEach?


I have this code to display a list of custom rows:

struct ContentView : View {
    var body: some View {
        VStack(alignment: .leading) {
            List(1...10) {_ in
                CustomRow()
            }
        }
    }
}

However, I want to remove the line on each row. I tried not using List and instead using ForEach inside ScrollView, but it completely removes all the styling, including its padding and margins. I just want to remove the lines and nothing else.

How can I do it?


Solution

  • iOS 15:

    In 2019, Apple introduced a new modifier .listRowSeparator that can be used to style the separators. You can pass .hidden to hide it:

    List {
        ForEach(items, id:\.self) {
            Text("Row \($0)")
                .listRowSeparator(.hidden)
        }
    }
    

    iOS 14:

    You may consider using a LazyVStack inside a ScrollView instead (because iOS is not supporting UIAppearance for SwiftUI lists anymore).

    LazyVStack Preview


    iOS 13:

    ⚠️ This method is deprecated and it's not working from iOS 14

    There is a UITableView behind SwiftUI's List for iOS 13. So to remove

    Extra separators (below the list):

    You need a tableFooterView and to remove

    All separators (including the actual ones):

    You need separatorStyle to be .none.

    Example of usage

    init() {
        if #available(iOS 14.0, *) {
            // iOS 14 doesn't have extra separators below the list by default.
        } else {
            // To remove only extra separators below the list:
            UITableView.appearance().tableFooterView = UIView()
        }
    
        // To remove all separators including the actual ones:
        UITableView.appearance().separatorStyle = .none
    }
    
    var body: some View {
        List {
            Text("Item 1")
            Text("Item 2")
            Text("Item 3")
        }
    }
    

    Note that a static list doesn't show extra separators below the list by default.