Computer >> Computer tutorials >  >> Programming >> IOS

How to resize an UIImageView using Swift?


To resize an image in iOS using swift we’ll make use of frame.

Let’s see this with help of an example.

  • Create an empty project and add an empty Image view.

  • Create its outlet.

  • Add an image to your project and assign the image to image view.
    How to resize an UIImageView using Swift?

Initially when we run the application, it looks something like this.

How to resize an UIImageView using Swift?

Now, let’s add code to resize the image.

override func viewWillLayoutSubviews() {
   let frame = CGRect(x: 10, y: 10, width: self.view.frame.width - 20, height: 300)
   self.imgView.frame = frame
}

We will run this code in our viewWillLayoutSubviews method. This is how it looks on the device when we run this.

How to resize an UIImageView using Swift?