且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除“行"SwiftUI 列表中的分隔符/分隔符?

更新时间:2023-02-19 14:03:56

iOS 15:

今年 Apple 推出了一个新的修饰符 .listRowSeparator,可用于设置分隔符的样式.你可以通过 .hidden 来隐藏它:

iOS 15:

This year 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

Apple 在 iOS 14 中引入了 LazyVStack.您可以考虑使用它代替列表:


iOS 14

Apple introduced LazyVStack In iOS 14. you may consider using it instead of list for this:

ScrollView {
    LazyVStack {
        ForEach((1...100), id: .self) {
           Text("Placeholder ($0)")
        }
    }
}

请记住,LazyVStack 是惰性的,不会一直渲染所有行.因此它们的性能非常好,并且是 Apple 自己在 WWDC 2020 中推荐的.

Keep in mind that LazyVStack is lazy and doesn't render all rows all the time. So they are very performant and suggested by Apple itself in WWDC 2020.

在适用于 iOS 的 SwiftUI 的 List 后面有一个 UITableView.所以要删除

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

你需要一个 tableFooterView 并删除

你需要 separatorStyle.none

init() {
    // 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")
    }
}