How to set width and height of an image in SwiftUI

2020-04-11 09:59发布

问题:

I am trying to set height and width of an image . I've tried using following code but it didn't work.

 Image("testImg")
  .frame(width: 50, height: 50)

回答1:

Try this

Image("testImg")
.resizable()
.frame(width: 50.0, height: 50.0)


回答2:

Image(restaurant.image)
            .resizable()
            .frame(width: 40, height: 40)
            .clipShape(Circle())


回答3:

Try this code:

 Image("testImg")
     .resizable()
     .scaledToFit()
     .frame(width: 50,height:50)


回答4:

  Image("testImg")
 .resizable()
 .scaledToFit()
 .frame(width: required-width,height:required-height)


回答5:

var body: some View {

            Image("yourImage")
                .resizable()
                .scaledToFit()
                .frame(width: 50.0, height:50.0)

    }


标签: swift swiftui