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

Time:11-30

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?
        
    }
}

CodePudding user response:

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.

  • Related