6

It seems there is no default separator in List by using SwiftUI on macOS. So I am using Divider to add by manual. 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 ? Or can I remove the default white line rendering by selection?

  1. add divider enter image description here

  2. multi selections with divider enter image description here

  3. multi selections without divider enter image description here

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()
                    }
                }
            }
        }
    }
}

6
  • I saw your code, it is kind of strange, why you are using List and ForEach together? one of them supposed to be there, also you are using Set for a List!?! that is not how should be! Why Set? why not array? Commented Jan 27, 2021 at 10:37
  • 1
    @swiftPunk I am using List and ForEach together because I want to show some sections in the list including some headers or footers. And using set for selection because it is specified by List interface. selection: Binding<Set<SelectionValue>>? Commented Jan 28, 2021 at 0:48
  • I know all what you said, I just wanted give you a general idea, otherwise there is some reason for sure in your codes Commented Jan 28, 2021 at 2:34
  • @swiftPunk ok, thank you for you help. Commented Jan 28, 2021 at 3:17
  • 1
    @user1046037 sorry, not yet Commented May 8, 2022 at 3:16

1 Answer 1

3

In macOS 13 or newer you can now use the listRowSeparator modifier like this:

List {
    ForEach(garage.cars) { car in
        Text(car.model)
            .listRowSeparator(.visible)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.