Search code examples
iosswiftautolayout

How to resize a fixed width and height UIImageView with auto layout


I have a view named myView which is always half the screen and sit at the top of screen.

Inside this view I added an UIImageView with 120x120 size which sits in center of the myView (horizontally and vertically). Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically.

But with a fixed height and width, the image doesn't resize when changing the screen size. I want my image to resize when running on iPhone5 or iPhone 4s, because myView will resize. I need something like the image should depend on the myView size.

How to actually achieve this ?


Solution

  • set imageView.clipToBounds = yes, and also set imageView.contentMode = UIViewContentModeScaleAspectFit;

    Update You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView