Reputation: 2483
I am trying to fit a square shaped image in rectangle shaped ImageView. With storyboard, I can easily do it with content mode .aspectFit but I am unable to figure out how to do it with SwiftUI.
I am currently doing something like this:
Image("BG")
.resizable()
.aspectRatio(contentMode: .fill)
.scaledToFill()
.clipped()
.frame(height: 200)
which gives below output, notice the blue border is my actual ImageView:
However, I am expecting something like this which I did in storyboard:
Upvotes: 3
Views: 455
Reputation: 257779
You need to clip it after set size, like
Image("BG")
.resizable()
.aspectRatio(contentMode: .fill)
.scaledToFill()
.frame(height: 200)
.clipped() // << here !!
Upvotes: 2