Search code examples
swiftswiftuiios14swiftui-listswiftui-tabview

List row separator not hiding when using another view on top


I am using a package that hides the list view separator for iOS 14. https://github.com/SchmidtyApps/SwiftUIListSeparator

Whenever I add another view on top of the List, like a simple Divider(), the list row separator will appear again for no reason.

var body: some View {
    
    VStack{
        
        Divider() //if I remove this divider, everything works fine
        
        List{
            ForEach(1...3, id: \.self){_ in
                
                Text("item")
                
            } .listSeparatorStyle(.none) ///the method to hide the row separator
            
        }
    }
}

You can try on your preview by yourself. I have tried multiple way to hide the separator but just this one worked for me, so please do not duplicate it.

Why whenever I add another view, the separator line appear again?

Also, how I can add the if #available(iOS 15, *)?

   var body: some View {
    
    VStack{
        Divider()
        
        List{
            ForEach(1...3, id: \.self){_ in
                
                Text("fdasf")
                
            }
            
            if #available(iOS 15, *) {
                .listRowSeparator(.hidden)
            }
            else{
                .listSeparatorStyle(.none)
            }
            
        }
    }
}

Solution

  • Per @Yodagama's comment, use .listRowSeparator(.hidden)

    To use different modifiers for OS versions, pull your List out into a function (or its own View), and then apply the relevant modifier…

    struct ContentView: View {
        
        var body: some View {
            if #available(iOS 15, *) {
                list()
                    .listRowSeparator(.hidden)
            } else{
                list()
                    .listRowSeparator(.hidden)
            }
        }
        
        func list() -> some View {
            List {
                ForEach(1...3, id: \.self){_ in
                    Text("fdasf")
                }
            }
        }
    }