0

Here's the simplest code I tried that acts wrong:

the grid has 10 red rectangles and 1 black rectangles

the grid has 6 red rectangles and 5 black rectangles

LazyVGrid(columns: Array(repeating: .init(.flexible(), alignment: .bottom), count: 7), spacing: 12) {
    ForEach(0..<5, id: \.self) { _ in
        Rectangle()
            .fill(.black)
    }

    ForEach(0..<10, id: \.self) { _ in
        Rectangle()
            .fill(.red)
    }
}

1
  • ForEach isn't a loop it's a View and for constant range there should be no id param Commented May 27 at 21:53

1 Answer 1

0

The issue lies in the id conflict between the two ForEach loops. Since the first few elements have duplicate ids, the lazy layout fails to render them correctly. After giving them elements unique id, the rendering issue fixed.

Sign up to request clarification or add additional context in comments.

1 Comment

The answer would be improved if you showed the corrected code.

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.