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

前端 未结 13 1969
盖世英雄少女心
盖世英雄少女心 2021-01-30 04:50

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

struct ContentView : View {
    var body: some View {
        VStack(alignment: .leading) {
            List(1         


        
13条回答
  •  萌比男神i
    2021-01-30 05:48

    iOS 13 builds only:

    While these answers are technically correct they will affect a List or Form globally(across the entire app) from my experience.

    A hacky way I found to resolve this problem, at least in my app, is to add the following code to the "main" content view of the app:

    .onAppear(perform: {
        UITableView.appearance().separatorStyle = .none
    })
    

    Then on any other view that you want to the separator lines add this to the end of the List or Form view

    .onAppear(perform: {
        UITableView.appearance().separatorStyle = .singleLine
    })
    

    This seems to add the single line separator to any view sheet that is above the main content view. Again this is all anecdotal to my recent SwiftUI experience.

    In my experience I only had to add the .onAppear(... = .singleLine) method to one of my "detail" views and the separator line appeared on all subsequent views that were presented.

    Edit: Another note as this answer continues to gain attention. This solution I posted doesn't solve all cases, it certainly did not solve it for me, again in some cases. I ended up using Introspect for SwiftUI to solve this problem across the entire app.

    I hope this clears up some confusion and frustration as people come across this post.

提交回复
热议问题