Salman500
Salman500

Reputation: 1211

SwiftUI view like UIKit UIView, How to change the size of the image

Is there any view in swiftui that is similar to UIKit UIView

There is an EmptyView in swiftui does anyone know what this view do

        Image("image")
        .resizable()
        .aspectRatio(3/2, contentMode: .fill)

This messes up the image is there any other way that i can resize the image

Upvotes: 1

Views: 2752

Answers (2)

Anjali Kevadiya
Anjali Kevadiya

Reputation: 3897

In SwiftUI, to change the image size there is 2 ways

1) Make it resizable and give some frame to Image

   Image("image name")
      .resizable()
      .frame(width: 10, height: 10, alignment: .center)

2) Make it resizable and scaleToFit property to Image

   Image("image name")
      .resizable()
      .scaledToFit()

For more customization of Image please look at below link https://developer.apple.com/documentation/swiftui/image/3269652-frame

I hope it will help you!!

Upvotes: 0

Rubaiyat Jahan Mumu
Rubaiyat Jahan Mumu

Reputation: 4127

In SwiftUI, primitive views like Color, Text, Image etc act like UIView. To change a image size you just need to give it a frame and make it resizable() first.

struct ContentView : View {

    var body: some View {

         Image("your image name")
            .resizable()
            .frame(width: 300, height: 300)
    }
}

Hope it will help.

Upvotes: 3

Related Questions