I've defined a custom layout container by having a struct
conform to Layout
and implementing the appropriate methods, and it works as expected.
The problem comes when trying to display Image
, as they are shown squished when just using the .resizable()
view modifier, not filling the container when using .scaledToFit()
or extending outside of the expected bounds when using .scaledToFill()
.
I understand that this is the intended behaviour of these modifiers, but I would like to replicate UIImageView
's scaledAspectFill
.
I know that this can usually be achieved by doing:
Image("adriana-wide")
.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.clipped()
But hardcoding the frame like that defeats the purpose of using the Layout
, plus I wouldn't have direct access to the correct sizes, either.
The only way I've managed to make it work is by having a GeometryReader
per image, so that the expected frame size is known and can bet set, but this is cumbersome and not really reusable.
GalleryGridLayout() {
GeometryReader { geometry in
Image("adriana-wide")
.resizable()
.scaledToFill()
.frame(width: geometry.size.width, height: geometry.size.height)
.clipped()
}
[...]
}
Is there a more elegant, and presumably efficient as well as reusable, way of achieving the desired behaviour?
Here's the code of the Layout
.
@Thecafremo Sorry for the delayed response, I was looking into this some more.
Using the GeometryReader is one approach, the other would be to reach down to UIKit and use a UIImageView and wrap that in a UIViewRepresentable.
Please file a bug report using Feedback Assistant for an API that allows you scale a View to an aspect ratio without stretching it.