简体   繁体   English

如何在 macOS 上的 SwiftUI 列表中正确实现分隔符

[英]How to implement separator in List by SwiftUI on macOS properly

It seems there is no default separator in List by using SwiftUI on macOS.在 macOS 上使用 SwiftUI 似乎列表中没有默认分隔符。 So I am using Divider to add by manual.所以我使用 Divider 手动添加。 But when I enable multiple line selection, the app will render a white line automatically.但是当我启用多行选择时,应用程序会自动呈现一条白线。 And it seems strange that both divider line and white line exist.而且分界线和白线都存在似乎很奇怪。

Is there a best practice way to implement row separator on macOS by using SwiftUI?是否有使用 SwiftUI 在 macOS 上实现行分隔符的最佳实践方法? Or can I remove the default white line rendering by selection?或者我可以通过选择删除默认的白线渲染吗?

  1. add divider添加分隔线在此处输入图像描述

  2. multi selections with divider带分隔线的多选在此处输入图像描述

  3. multi selections without divider多选不带分隔符在此处输入图像描述

struct WelcomeView: View {
    let contents = ["aaa", "bbb", "ccc"]
    @State var selection = Set<String>()

    var body: some View {
        List(selection: $selection) {
            Section(header: Text("header")) {
                ForEach(contents, id: \.self) { row in
                    VStack {
                        Text(row)

//                        Divider()
                    }
                }
            }
        }
    }
}

In macOS 13 or newer you can now use the listRowSeparator modifier like this:在 macOS 13 或更高版本中,您现在可以像这样使用 listRowSeparator 修饰符:

List {
    ForEach(garage.cars) { car in
        Text(car.model)
            .listRowSeparator(.visible)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM