How to resize an UIImageView using Swift?

iOSMobile DevelopmentiPhone/iPadSwift

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.

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

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.

Published on 18-Apr-2019 10:08:27