2

I've noticed that list separators in iOS 16 are behaving more like normal table views, but I've found a few instances where they don't seem to be working properly.

They seem to start wherever they find a text object. In most circumstances, this is fine, but I have a use case where this doesn't look good, where I have some text in a ZStack inside a circle.

Does anyone know a workaround?

struct ContentView: View {
    var numbers = 1...10
    var body: some View {
        List
        {
            Section("Images") {
                ForEach(numbers, id: \.self) { number in
                    HStack {
                        Image(systemName: "\(number).circle")
                        Text(String(number))
                    }
                }
            }
            
            Section("Circles") {
                ForEach(numbers, id: \.self) { number in
                    HStack {
                        ZStack {
                            Circle()
                                .strokeBorder(.black, lineWidth: 2)
                                .frame(width: 20, height: 20)
                            Text(String(number))
                                .font(.caption)
                        }
                        Text(String(number))
                    }
                }
            }
        }
        .listRowInsets(EdgeInsets())
        .padding()
    }
}

enter image description here

2
  • 1
    You can change the insets. See this answer. Commented Sep 26, 2022 at 17:21
  • 1
    This doesn't work. It will add insets to the content, but the separators don't change. Commented Sep 26, 2022 at 17:41

1 Answer 1

1

You can override the default behavior by setting the .listRowSeparatorLeading alignment guide on the view you want the divider to be aligned to. For example .alignmentGuide(.listRowSeparatorLeading) { $0[.leading] } will align the divider leading to whatever view you add the modifier to.

So in your case:

@available(iOS 16, *)
struct ContentView: View {

    var numbers = 1...10

    var body: some View {
        List {
            Section("Images") {
                ForEach(numbers, id: \.self) { number in
                    HStack {
                        Image(systemName: "\(number).circle")
                            .alignmentGuide(.listRowSeparatorLeading) { $0[.leading] }
                        Text(String(number))
                    }
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

To get back to the original way, you can also add the .alignmentGuide(.listRowSeparatorLeading) modifier to the HStack and simply return 0 in the closure. sarunw.com/posts/swiftui-list-row-separator-insets

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.