I'm desperately trying to change the aspect ratio of an image to 16:9 without stretching it. .aspectRatio(16/9, contentMode: .fill or .fit)
isn't usable because it's pure garbage (no one on Earth wants to stretch an image). Also, there is no difference between CGFloat
or CGSize
.
So I have first tried something like this:
Button("Button", action: { })
Image("example")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: UIScreen.main.bounds.width - x, height: (UIScreen.main.bounds.width - x) * 0.5625)
// .border(Color.red)
.clipped()
And it seems to be right but actually not at all, the hidden part of the image goes beyond the button. Strangely, the button is visible but of course not usable. You can see this by yourself if you uncomment .border(Color.red)
and comment .clipped()
.
Then, I have tried the 2 solutions from: https://alejandromp.com/blog/image-aspectratio-without-frames/ but it's actually the exact same result.
Please, tell me you know how to deal with this issue? A func? A custom view? Thanks!