UITableView().appearance().separatorStyle = .none
在iOS 13中这样做是有效的。然而,现在它不起作用了。有没有更新或者有什么办法可以让它起作用呢?谢谢 :)Color(UIColor.systemBackground)
代替Color.white
吗? - akmin.listRowInsets(EdgeInsets(.init(top: -1, leading: -1, bottom: -1, trailing: -1)))
。 - zrfrank将@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()
}
LazyVStack
就不太适合了。 - Yiming Dong我如何创建一个适用于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()
}
}
}
希望这篇文章能够帮到有需要的人,我在一个小问题上浪费了很多时间。苹果似乎在极力推动我们使用LazyVStack。
我在苹果开发者论坛上找到了这个简单的解决方案。它适用于14.4版本,对我有效:
List {
...
}.listStyle(SidebarListStyle())
这似乎在边缘周围添加了一点填充。如果对您来说是个问题,您可以尝试一些负填充。
根据Average Joe的答案,我最终得到了以下修改器:
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 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 的编辑功能。
此解决方案不幸依赖于硬编码的值,以删除每行的系统默认填充。希望 SwiftUI 3.0 提供简单的 .separatorStyle(.none) 和 .accessoryType(.none) 修饰符。
去除披露指示器的代码来自:https://www.appcoda.com/hide-disclosure-indicator-swiftui-list/
extension View {
func hideListRowSeperator() -> some View {
if #available(iOS 15, *) {
return AnyView(self.listRowSeparator(.hidden))
} else {
return AnyView(self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.listRowInsets(EdgeInsets(top: -1, leading: -1, bottom: -1, trailing: -1))
.background(Color(.systemBackground)))
}
}
}
使用示例
var body: some View {
List {
ForEach(0..<3) { _ in
Text("Hello, World!")
.padding(.leading)
.hideListRowSeperator()
}
}
.listStyle(.plain)
}
更新:
我找到了一个解决方案,可以在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
}
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()
}
}
}
SceneDelegate
? - Ovi Trif.environment(\.defaultMinListRowHeight, 1)
。 - Harish saini如果您的单元格数量不多,因此不需要依赖LazyVStack来提高性能,那么您可以使用ScrollView + VStack:
ScrollView {
VStack {
Row1()
Row2()
Row3()
}
}
UITableView
了...也没有钩子)) - Asperi