简体   繁体   中英

SwiftUI Unable to resize Image

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM