Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-Layout Constraint: How to make a view maintains its width/height ratio when resized?

In the interface builder we can pin height, pin width, make two views width equally, but how do I set the constraints so that when a view is being resized, it maintains its width/height ratio?

In my particular case, I have an UIImageView in my view controller. When the view resizes, I'd like my image view to resize, but maintain a 3:2 width:height ratio. Is it possible to do it in IB? Is it possible to do it with code?

Thanks!

like image 962
Joseph Lin Avatar asked Jan 07 '13 00:01

Joseph Lin


People also ask

What is Autolayout aspect ratio?

If you select Aspect Ratio for multiple items, Auto Layout chooses the width of one of the items for the numerator and the height of another item for the denominator. To change the initial aspect ratio, edit the Multiplier field of the Attributes inspector for the constraint.

What are auto layout constraints?

Constraints allow you to control how objects respond to changing frames, while auto layout allows you to control how frames respond to changing objects. In this article, I'll explain how they work and when to use each.

What is vary trait in auto layout?

Trait Variation is a change to the presentation of your user interface that is based on a device configuration. Trait Variations of the user interface is not just limited to constraints but can be applied to much more.


1 Answers

You can add an aspect ratio constraint in IB by control dragging from the view to itself and choosing aspect ratio.

like image 141
rudi Avatar answered Sep 21 '22 17:09

rudi