所以这个问题很简单,它在标题中。我想在 SwiftUI iOS 14 中删除行分隔符。以前,我正在使用
UITableView().appearance().separatorStyle = .none
并且曾经在 iOS 13 中完成这项工作。但是,现在它不起作用。有关如何使其工作的任何更新或想法。谢谢:)
11 回答
我如何制作一个适用于 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())
}
}
}
示例用法 -
ListWithoutSepatorsAndMargins {
ForEach(0..<5) { _ in
Text("Content")
}
}
如果列表中有更多组件,请将它们包装在 Group
ListWithoutSepatorsAndMargins {
Group {
self.groupSearchResults()
self.myGroups()
self.exploreGroups()
}
}
}
希望这对某人有所帮助,我在这些小事上浪费了很多时间,Apple 正在努力推动我们使用 LazyVStack,看来
将@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()
}
我在 Apple Developer 论坛上找到了这个简单的解决方案。它在 14.4 上为我工作:
List {
...
}.listStyle(SidebarListStyle())
这似乎在边缘周围添加了一点填充。如果这对您来说是个问题,您可以尝试一些负填充。
struct ListSeparatorNone: ViewModifier {
var backgroundColor: Color = Color(.systemBackground)
func body(content: Content) -> some View {
content
.listRowInsets(EdgeInsets(top: -1, leading: 0, bottom: 0, trailing: 0))
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.background(backgroundColor)
}
}
视图扩展:
extension View {
func listSeparatorNone(backgroundColor: Color = Color(.systemBackground)) -> some View {
self.modifier(ListSeparatorNone(backgroundColor: backgroundColor))
}
}
使用示例:
List {
ForEach(viewModel.countries, id: \.self) { country in
Text(country)
.padding(.leading, 10)
}
.listSeparatorNone()
}
iOS 15:
今年 Apple 推出了一种新的修饰符.listRowSeparator
,可用于设置分隔符的样式。你可以通过.hidden
隐藏它:
List(items, id:\.self) {
Text("Row \($0)")
.listRowSeparator(.hidden)
}
您也可以按照我在此答案listRowSeparatorTintColor
中提到的设置将每个分隔符设置为任何颜色:
iOS 14
按照这里的答案
您也可以在 VStack 的末尾(即 List 内部)调用此函数。
它将是 iOS 14 上列表分隔符的叠加层 :)
private func hideDefaultListSeperator() -> some View {
Rectangle()
.fill(colorScheme == .light ? Color.white : Color.black)
.frame(maxHeight: 1)
}
这是我的 iOS 14 解决方案:
struct MyRowView: View {
var body: some View {
ZStack(alignment: .leading) {
// Background color of the Row. It will spread under the entire row.
Color(.systemBackground)
NavigationLink(destination: Text("Details")) {
EmptyView()
}
.opacity(0) // Hide the Disclosure Indicator
Text("Go to Details").padding(.leading)
}
// These 2 lines hide the row separators
.padding(.horizontal, -16) // Removes default horizontal padding
.padding(.vertical, -6) // Removes default vertical padding
}
}
封闭的列表应该有这个修饰符
.listStyle(PlainListStyle())
与使用LazyVStack 相比,此解决方案的优势在于您仍然可以使用List的 Edit 功能。
不幸的是,该解决方案依赖于硬编码值来删除每行上的系统默认填充。希望 SwiftUI 3.0 将提供简单的 .separatorStyle(.none) 和 .accessoryType(.none) 修饰符。
删除 Disclosure Indicators 的代码来自:https ://www.appcoda.com/hide-disclosure-indicator-swiftui-list/
更新:
我想出了一个适用于 iOS 13 和 iOS 14 的解决方案,并提供了一个简单的列表并在两个 iOS 上都使用 List。
struct ListWithoutSepatorsAndMargins<Content>: View where Content: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
List {
self.content()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.listRowInsets(EdgeInsets())
.background(Color.white)
}
.listStyle(PlainListStyle())
.buttonStyle(NoButtonStyle())
}
}
struct NoButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
}
并在 SceneDelegate.swift 中执行以下操作以删除单元格的默认灰色选择
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
UITableView.appearance().separatorStyle = .none
UITableView.appearance().allowsSelection = false
.......
我们可以通过以下方式使用它
ListWithoutSepatorsAndMargins {
ForEach(0..<5) { _ in
Text("Content")
}
}
ListWithoutSepatorsAndMargins {
Group {
self.groupSearchResults()
self.myGroups()
self.exploreGroups()
}
}
}
如果您没有很多单元格,因此不需要依赖 LazyVStack 来获得性能,您可以回退到 ScrollView + VStack:
ScrollView {
VStack {
Row1()
Row2()
Row3()
}
}
上面的答案对我有用,你只需要在这两个函数下面设置:
.listRowInsets(EdgeInsets())
.background(Color.white)