Reputation: 5420
I am trying to create image in SwiftUI and the problem I am facing is that image is not resizing itself even if I set content mode fill
struct LoginView: View {
var body: some View {
Image("app-icon")
.frame(width: percent(80, ofSize: "width"), height: percent(40, ofSize: "width"), alignment: Alignment.center)
.aspectRatio(contentMode: ContentMode.fill)
.scaledToFill()
.clipped()
}
}
Then I tried
struct LoginView: View {
var body: some View {
Image("app-icon")
.frame(width: percent(80, ofSize: "width"), height: percent(40, ofSize: "width"), alignment: Alignment.center)
.aspectRatio(contentMode: ContentMode.fit)
.scaledToFit()
.clipped()
}
}
Both Code has same effect image size is not changed
Upvotes: 1
Views: 774
Reputation: 4875
Add .resizable()
modifier to your code
Image("app-icon")
.resizable()
.scaledToFill()
.frame(width: percent(80, ofSize: "width"), height: percent(40, ofSize: "width"), alignment: Alignment.center)
.clipped()
Upvotes: 2
Reputation: 14388
You have to add .resizable()
as the first modifier after Image for the other modifiers to have any effect.
Upvotes: 2