2

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 ?

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • have you tried setting contentMode of imageView? imageView.contetnMode = UIVIewContentModeAspectFill; – Adnan Aftab Aug 16 '15 at 14:53
  • Yes, but I think because the height and width are fixed, it won't change anything. – Adrian Aug 16 '15 at 15:01
  • u can do this using auto layout...but may be a bit messy... Simply you can set the outlet of the fixed height and width constraint..then set it programmatically...in the viewWillAppear() function. – Sujith Chandran Aug 16 '15 at 15:31

4 Answers4

5

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

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • It doesn't resize, I guess it's because of fixed width and height. – Adrian Aug 16 '15 at 15:05
  • Okay, if you are taking about imageView not the image then it will not resize – Adnan Aftab Aug 16 '15 at 15:06
  • Ok, if I choose aspect ration with superview, it will give me a 2:5 ratio. But I still need to give it more constraints to the imageview. For me, leading/trialing/bottom/top seems to not work. It always ask me for X and Y constraint. Any idea what more constraints I can give to the imageview ? – Adrian Aug 16 '15 at 15:15
  • If you are using leading, top, bottom and trailing then remove height and width constraint and it will work.... – Adnan Aftab Aug 16 '15 at 16:19
  • Its not working, the imageview doesn't even appear on the screen anymore. Very strange, in the view I also have a label and some buttons, maybe they are affecting it. – Adrian Aug 17 '15 at 15:03
  • I managed to add constraints like proportional width and height to superview, by changing the Multiplier value. And also setting center to X and Y. Now it appears in the center, but it's no longer a circle. It looks like it is pushed from the sides. And there is no auto layout error. – Adrian Aug 17 '15 at 15:14
  • Chnage radius value inside viewdidlayoutsubviews imageView.layer.cornerRadius=width/2 – Adnan Aftab Aug 17 '15 at 15:28
  • I am already doing that, but in viewDidLoad. Want to discuss this on chat ? – Adrian Aug 17 '15 at 15:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87179/discussion-between-c-x-and-adrian). – Adnan Aftab Aug 17 '15 at 15:34
  • 1
    literally just spent an hour trying to manually update the `Constant` value of two NSLayoutConstraints (one for width, one for height), without any progress and the `contentMode` was the answer. i would upvote this 5 times if i could. – cscott530 Sep 19 '17 at 14:17
0

If you set the image with fixed width and height it will not change respectively. You can either set it to be relative to myView or you can instead set leading and trailing size from all for edges and delete the center constrains.

Or Ron
  • 2,313
  • 20
  • 34
  • How can I set it to be relative to myView ? – Adrian Aug 16 '15 at 15:02
  • You need to play with the constrains it should take much time. you can also set fixed trailing and leading space would create similar result. – Or Ron Aug 16 '15 at 15:05
0

Your constraints should look like this:

enter image description here

orkenstein
  • 2,810
  • 3
  • 24
  • 45
  • If I do this, it will say for example leading and trailing = 240 , because of the free form of the storyboard, which is much bigger then the screen. And then when running, my image view disappears from the screen. – Adrian Aug 16 '15 at 15:10
  • Sorry, I can not understand your layout, maybe you can attach a screenshot? @Adrian – orkenstein Aug 16 '15 at 15:14
  • Its exactly your layout from the screenshot., but the image view is much smaller and in the center of the view. The size of the storyboard is Inferred. Which is much wider than the screen size when running in simulation. – Adrian Aug 16 '15 at 15:16
  • @Adrian No problem, change view size from **inferred** to any **iphone**. And then establish constraints. – orkenstein Aug 16 '15 at 15:21
0

For Swift 4: Updating answer of @Adnan Aftab

imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
FARAZ
  • 645
  • 7
  • 6