Related to this: SwiftUI duplicated border in VStack/HStack
How can I avoid a duplicated border in a grid if I do not draw every single tile? The proposed solution seems to only work properly if all borders between tiles are drawn.
struct LeastQueensView: View {
var tileList: [(Int, Int)] = [(1,1), (1,2), (1,3), (2,3), (2,1), (3,3), (3,4)]
var maxX = 3
var maxY = 4
var body: some View {
grid
}
var grid: some View {
Grid(horizontalSpacing: 0, verticalSpacing: 0) {
ForEach((1...maxX).reversed(), id: .self) { i in
GridRow {
ForEach(1...maxY, id: .self) { j in
if tileList.contains(where: {$0 == (i,j)}) {
ZStack {
Rectangle()
.foregroundColor(.gray)
.aspectRatio(1.0, contentMode: .fit)
Rectangle()
.strokeBorder(lineWidth: 1)
.aspectRatio(1.0, contentMode: .fit)
}
}
else {
Rectangle()
.aspectRatio(1.0, contentMode: .fit)
.foregroundColor(.white)
}
}
}
}
}
.padding()
}
}
All borders in between grey tiles are doubled while borders adjacent to a white tile are not.