Varun Naharia
Varun Naharia

Reputation: 5420

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()

    }
}

enter image description here

Both Code has same effect image size is not changed

Upvotes: 1

Views: 774

Answers (2)

Rohit Makwana
Rohit Makwana

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

LuLuGaGa
LuLuGaGa

Reputation: 14388

You have to add .resizable() as the first modifier after Image for the other modifiers to have any effect.

Upvotes: 2

Related Questions