Add a border with cornerRadius to an Image in SwiftUI Xcode beta 5

前端 未结 5 601
一向
一向 2021-02-05 12:08

how can I add a border with a cornerRadius to an Image. I get a deprecation warning saying that i should use a RoundedRectange Shape, but i don\'t know how to use t

5条回答
  •  执念已碎
    2021-02-05 13:00

    First, note that the way you were doing it, was not clipping the image. Maybe you did not noticed if the image was too small, or if it had a background of the same color of your canvas. But even while using your beta 4 syntax, you needed to add .clipShape().


    Back to your question, according to Beta 5 release notes:

    Complex overloads for the background(:alignment:) and border(:width:) modifiers are deprecated. Use shapes in a background(:alignment:) or overlay(:alignment:) to draw these instead. (53067530)

    So the pattern would be something like this:

    .overlay(RoundedRectangle(...).stroke(...).foregroundColor(...))
    

    In your specific case:

    Image("mypic").resizable().frame(width: 300, height: 300)
        .clipShape(RoundedRectangle(cornerRadius: 30))
        .overlay(RoundedRectangle(cornerRadius: 30).stroke(lineWidth: 2).foregroundColor(Color.black))
    

提交回复
热议问题