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
You have to add .resizable()
as the first modifier after Image for the other modifiers to have any effect.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.