8

I have a UIImageView in a UIViewController's view. What I want is to set it's height 1/3rd that of the view no matter what the screen size is. I of course know how to do this from code. But how to accomplish this using Storyboard/Interface builder only ?

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • 1
    You can easily do that with auto layout. Add an height constraint to image view with some aspect ratio. – Ozgur Vatansever Sep 26 '16 at 05:16
  • @ozgur : But if I set the4 height constraint, it's height would be always same for all devices. Won't it ? – Nitish Sep 26 '16 at 05:18
  • No. This is why I mentioned about *aspect ratio*. You are setting height with respect to its superview's height. So it will grow as the superview grows. Please take a look at [this SO question](http://stackoverflow.com/questions/25766747/emulating-aspect-fit-behaviour-using-autolayout-constraints-in-xcode-6). – Ozgur Vatansever Sep 26 '16 at 05:19
  • 1
    @ozgur : Many thanks. Feel sorry for myself that didn't know about this constraint. Quite useful.You might wanna add an answer if you wish. – Nitish Sep 26 '16 at 05:28

1 Answers1

19

Add a height constraint whose multiplier value is 1/3 so the image view's height will be one third of its superview's height.

enter image description here

--

Here is how it looks with different device sizes:

Bad rendering on small screen    Bad rendering on small screen     Bad rendering on small screen    

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119