How to remove List Separator lines in SwiftUI 2.0 in iOS 14

后端 未结 6 733
谎友^
谎友^ 2020-12-15 17:07

So the question is pretty simple and it\'s in the title. I want to remove the line separator in SwiftUI iOS 14. Previously, I was using UITableView().appearance().sep

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 18:01

    How I made a list that works on both iOS 14 and iOS 13, It shows no separators and extra margins

    struct NoButtonStyle: ButtonStyle {
        func makeBody(configuration: Self.Configuration) -> some View {
            configuration.label
        }
    }
    
    struct ListWithoutSepatorsAndMargins: View {
            let content: () -> Content
        
            var body: some View {
                if #available(iOS 14.0, *) {
                    ScrollView {
                        LazyVStack(spacing: 0) {
                            self.content()
                        }
                        .buttonStyle(NoButtonStyle())
                    }
                } else {
                    List {
                        self.content()
                    }
                    .listStyle(PlainListStyle())
                    .buttonStyle(NoButtonStyle())
                }
            }
        }
    

    Sample Usage -

    ListWithoutSepatorsAndMargins {
        ForEach(0..<5) { _ in
          Text("Content")
        }
    }
    

    in case you've more components in list, wrap them in Group

                    ListWithoutSepatorsAndMargins {
                        Group {
                                self.groupSearchResults()
                                self.myGroups()
                                self.exploreGroups()
                            }
                        }
                    }
    
        
    

    Hope this helps someone, I wasted a lot of time in such minor thing, Apple is trying to push us hard to use LazyVStack, it seems

提交回复
热议问题