2

How can I, without using Section, achieve a list that looks like this:

List with separated members --> This list has separated members

List with no separation --> This list has no separated members

Here is how the list is created, using Section:

List {
    ForEach(item, id: \.self) { item in
        Section {
            NavigationLink {
                VStack {
                    Text(name ?? "Err").font(.title)
                    Text(category ?? "Err").font(.subheadline)
                }
            } label: {
                HStack {
                    Text("\(order)")
                    Spacer()
                    Text(name ?? "Err")
                }
            }
        }
    }
}
.listStyle(.insetGrouped)
9
  • What's wrong with using Section? Commented Jul 25, 2022 at 15:12
  • I don't understand your goal here. You either using Section or Something Else requires more code. Also, if you want more customizable just go with ForEach{} + HStack like this answer: stackoverflow.com/questions/66222068/… Commented Jul 25, 2022 at 15:17
  • @DávidPásztor it's extremely choppy/laggy looking when editing the list, moving items up and down on the list Commented Jul 25, 2022 at 15:17
  • ForEach and padding Commented Jul 25, 2022 at 15:18
  • 1
    Yes you are required. Swiftui List is very limited in term of design customization, so that’s your only choice. Commented Jul 25, 2022 at 15:28

1 Answer 1

2

A possible solution for this is to turn off default row background and separators and make row drawing by ourselves using row insets to manage distances between rows... and this will not break default List bahavior.

Tested with Xcode 13.4 / iOS 15.5

demo

List {
    ForEach([1, 2, 3], id: \.self) { item in
        NavigationLink {
            VStack {
                Text("name \(item)").font(.title)
            }
        } label: {
            HStack {
                Text("\(item)")
                Spacer()
                Text("name \(item)").font(.title)
            }
        }
        .padding(.horizontal)
    }
    .background(RoundedRectangle(cornerRadius: 12).fill(.red))
    .listRowBackground(Color.clear)
    .listRowSeparator(.hidden)
    .listRowInsets(EdgeInsets(top: 4, leading: 0, bottom: 4, trailing: 0))
}
.listStyle(.insetGrouped)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this is a fantastic answer! I wish I had enough reputation to upvote :)
This is a great answer I agreed. Btw @joker you can click "correct answer" without any required reputation.

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.