Why I cannot use strokeBorder on content that conforms to Shape in SwiftUI?

I have a CustomShapeView which basically takes a Shape content, so after importing in to CustomShapeView I want use a modifier that works on Shape, but in my case it does not! I am wondering what I am missing in between?

My Goal: I want to be able to import a Shape and apply strokeBorder modifier inside CustomShapeView and return it as a view.

struct ContentView: View {
    var body: some View {

        CustomShapeView(shapeContent: {
            
            RoundedRectangle(cornerRadius: 50.0)

        })
        .padding()
    }
}


struct CustomShapeView<ShapeContent: Shape>: View {
    
    let shapeContent: () -> ShapeContent
    
    var body: some View {

      return shapeContent()
            //.strokeBorder(style: StrokeStyle(lineWidth: 10.0))  // <<: Here!!! Why?
        
    }
}

1 answer

  • answered 2021-11-29 04:55 Raja Kishan

    Set InsettableShape generic constraint.

    struct CustomShapeView<ShapeContent: InsettableShape>: View { // << == Here
        
        let shapeContent: () -> ShapeContent
        
    

    Just go to the strokeBorder definition, where strokeBorder is defined inside the InsettableShape protocol and which is inherited Shape.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum