Search code examples
swiftiphoneswiftuibetaxcode11

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


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 that exactly

Beta 4:

Image(uiImage: ...)
   .border(Color.black, width: 2, cornerRadius: 10)

Solution

  • SwiftUI 5.0

    Using clipShape & overlay Modifiers

    Here is another way in which we can use a clipShape modifier (which clips the view) and then overlay a stroke with a color.

    VStack(spacing: 40) {
        Text("Image Border").font(.largeTitle)
        Text("Using cornerRadius & overlay").font(.title).foregroundColor(.gray)
        Text("Using cornerRadius will also clip the image. Then overlay a border.")
            .frame(maxWidth: .infinity)
            .font(.title)
            .padding()
            .background(Color.orange)
            .foregroundColor(.black)
        
        Image("profile")
            .clipShape(.rect(cornerRadius: 10))
            .overlay(.rect(cornerRadius: 10)
                .stroke(Color.orange, lineWidth: 4))
            .shadow(radius: 10)
    }
    

    Result

    screenshot of clipshape