SwiftUI set corner radius depending on its own height

861 Views Asked by At

When I set corner radius with constant value, depending on the size of the image, the results don't come out the way I want.

Image(“myImage”)
    .cornerRadius(25)

What I want in app. enter image description here More clipped in Widget enter image description here So I want to set radius value like image.size.height / 5, depending on its own size.

When I think about overlay, code will be like below,

Image("myImage")
.overlay(
    GeometryReader { geo in
        RoundedRectangle(cornerRadius: geo.size.height / 5)
    }
)

but when it comes to clipShape to apply corner radius, I don't know how to do.

1

There are 1 best solutions below

0
On

Try using clip shape with Circle as shape - Using it with an aspect ratio will give you a perfectly square image and content mode will make sure the image fits perfectly.

Use something like this -

Image(“myImage”)
    .aspectRatio(1, contentMode: .fit)
    .clipShape(Circle())