如何在 iOS 14 中删除 SwiftUI 2.0 中的列表分隔符行

Osa*_*eem 29 xcode list separator swiftui ios14

所以问题很简单,就在标题中。我想在 SwiftUI iOS 14 中删除行分隔符。以前,我 UITableView().appearance().separatorStyle = .none 在 iOS 13 中使用 和使用它来完成这项工作。但是,现在它不起作用。有关如何使其工作的任何更新或想法。谢谢:)

Asp*_*eri 40

这是可能的解决方案的演示。使用 Xcode 12b 测试。

演示

List {
    ForEach(0..<3) { _ in
        VStack {
            Text("Hello, World!").padding(.leading)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
        .listRowInsets(EdgeInsets())
        .background(Color.white)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用“Color(UIColor.systemBackground))”而不是“Color.white”? (4认同)
  • 此外,在某些情况下您可能希望应用负边缘插入。`.listRowInsets(EdgeInsets(.init(顶部:-1,前导:-1,底部:-1,尾随:-1)))`。 (3认同)
  • 如何在不修改插图的情况下做到这一点?删除所有填充会使列表看起来很糟糕。 (2认同)

ave*_*Joe 10

将@asperi、@akmin 和@zrfrank 答案合并为一件事。根据我的经验,List它比 更可靠、更高效LazyVStack,因此我仍将其用于List任何需要可靠性的复杂情况。

extension View {
    func listRow() -> some View {
        self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
            .listRowInsets(EdgeInsets(top: -1, leading: -1, bottom: -1, trailing: -1))
            .background(Color(.systemBackground))
    }
}

List {
    Color.red
         .listRow()


    Color.green
         .listRow()
}
Run Code Online (Sandbox Code Playgroud)


Moj*_*ini 9

iOS 15:

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

List(items, id:\.self) {
    Text("Row \($0)")
        .listRowSeparator(.hidden)
}
Run Code Online (Sandbox Code Playgroud)

您还可以通过设置将每个分隔符设置为任何颜色,listRowSeparatorTintColor正如我在这个答案中提到的:

演示

iOS 14

按照这里的答案


Har*_*ini 7

我如何制作一个适用于 iOS 14 和 iOS 13 的列表,它不显示分隔符和额外的边距

struct NoButtonStyle: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
    }
}

struct ListWithoutSepatorsAndMargins<Content: View>: 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())
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

示例用法 -

ListWithoutSepatorsAndMargins {
    ForEach(0..<5) { _ in
      Text("Content")
    }
}
Run Code Online (Sandbox Code Playgroud)

如果列表中有更多组件,请将它们包装在 Group 中

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

    
Run Code Online (Sandbox Code Playgroud)

希望这对某人有所帮助,我在这么小的事情上浪费了很多时间,Apple 正在努力推动我们使用 LazyVStack,看来

  • 这个解决方案是有害的,因为 LazyVStack 以与 List 完全不同的方式处理单元。您可以在滚动这些单元格时检查 CPU 负载。如果单元格不仅仅是文本(“hello test”),您将在滚动时看到峰值负载 (3认同)
  • @Zapko 你觉得 UIKit 中 StackView 和 TableView 的主要区别是什么?List 和 LazyVStack 也有同样的情况。TableView 重用单元格,而 LazyVStack 则不重用。因此,当您有更多适合屏幕的元素,并且布局比 Text("Content") 更复杂时,您将变得极其缓慢和滞后。因此,提供名为 ListWithoutSeparators{} 的解决方案并使其内部成为 LazyVStack 是非常有害的。 (2认同)
  • 通过使用惰性 vstack,您会失去其他列表优势,例如编辑状态、滑动删除手势等。 (2认同)

Mar*_*ips 5

我在苹果开发者论坛上找到了这个简单的解决方案。它在 14.4 上对我有用:

List {
   ...
}.listStyle(SidebarListStyle())
Run Code Online (Sandbox Code Playgroud)

这似乎在边缘周围添加了一点填充。如果这对您来说是个问题,您可以尝试一些负填充。