I would like to stack overlapping elements horizontally, like this:
Here is what I tried:
struct StackedElementsView: View {
let colors: [Color] = [.red, .blue, .purple]
var body: some View {
HStack {
ZStack {
ForEach(0..<colors.count) { i in
ZStack(alignment: .leading) {
colors[i]
.clipShape(Circle())
.frame(width: 44, height: 44)
}
.offset(x: CGFloat(i) * 25)
}
}
.padding(.leading, 24)
Color.purple
.frame(width: 100, height: 44)
Spacer()
}
.padding(.vertical, 8)
}
}
struct StackedElementsView_Previews: PreviewProvider {
static var previews: some View {
StackedElementsView()
}
}
I have an issue with this, the purple rectangle overlaps the circles and I don't know why.
Thank you for your help

