0

I have a controller in which there are various questions and images.

In each question all the elements (images, button) are shifted relative to the size of the text. But I also have pictures of different sizes. How as one ImageView show pictures of different size correctly, so that they adjust to the screen?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

2 Answers2

3

If you want image to show on full size of imageView you should use this:

this will scale image on Aspect Fill

imageView.contentMode = UIViewContentModeScaleAspectFill;

also there are few more Keywords: AspectFit, ScaleToFill

swift 2.0 version:

imageView.contentMode = .ScaleAspectFill

also you can add autoresizingMask:

imageView.autoresizingMask =   
    ( UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleHeight
    | UIViewAutoresizingFlexibleLeftMargin
    | UIViewAutoresizingFlexibleRightMargin
    | UIViewAutoresizingFlexibleTopMargin
    | UIViewAutoresizingFlexibleWidth );
C0mrade
  • 1,215
  • 1
  • 10
  • 23
0

First, change contentMode to UIViewContentModeScaleToFill to fill in encapsulated image:

myImageView.image = myImage;
myImageView.contentMode = UIViewContentModeScaleToFill;

Second, adjust image view frame per current image

CGRect frame = myImageView.frame;
frame.size = myImage.size;
myImageView.frame = frame;
Abhinav
  • 37,684
  • 43
  • 191
  • 309