ios – SwiftUI – Shapes in ForEach view with completely different stroke coloration and line width Not Working


I’ve an array of Path and I’ve a customized form.

struct MyShape: Form {
    var path: Path

    func path(in rect: CGRect) -> Path {

I’m drawing a number of shapes utilizing a ForEach view. I need to assign completely different line widths and colours to every view

struct MyView: View {

    @State non-public var paths: [Path] = []
    @State non-public var lineWidths: [Double] = []
    @State non-public var colours: [Color] = []

    var physique: some View {
        ZStack {
            ForEach(0..<paths.depend, id: .self) { index in
                MyShape(path: paths[index])
                    .stroke(lineWidth: lineWidths[index])


The paths are drawing advantageous however the entire shapes are taking linewidth and coloration the identical as the primary form. For instance, if I’ve the colours

@State non-public var lineWidhts: [Double] = [4.0, 10.0, 20.0]
@State non-public var colours: [Color] = [,, Color.yellow]

The entire shapes have 4.0 line width and pink coloration as an alternative of getting completely different colours.


Leave a Reply