What is the Nslayoutconstraint "Aspect Ratio" for?

Asked

Viewed 61 times

2

Hello,

Since I started working with Autolayout, I couldn’t find anything that would explain how/for it NSLayoutConstraint Aspect Ratio. Even in the Apple documentation I couldn’t find anything to help you understand.

Someone has an example to share?

1 answer

1


I ended up formulating the question in English, the answer was quite useful: https://stackoverflow.com/questions/43123670/for-what-should-i-use-the-aspect-ratio-nslayoutconstraint

UPDATE

Explanation in Portuguese, translated from the reply given:

I have an example of how to keep a square centered on all devices and screen orientations.

The constants are configured in IB, but you can also do it by code. Anyway, the goal here is to explain how to prioritize margin constants to make Autolayout know what it should break and when. What keeps the square as it is is its ratio (Aspect ratio) - it is 1:1. If it was a rectangle, change to 2:1 (or 1:2, depending on your need).

When you understand these two parts (prioritization and proportion), the last part is the margin values - I set it to 10 points, which means that the square will have 10 margin points on its smallest axis. If the device is an iPad Pro 12.9", you will have a very wide square. If this same device is an iPhone SE, you will have, on the other hand, a smaller square. Regardless of the situation, you will have a margin of 10 points. Increase the margin to 50 and the square will be relatively smaller. Anyway here, the important thing is that you will always have a 1:1 ratio, this view will always be a square..

Completion

To NSLayoutConstraint Aspect Ratio is the constant related to proportion of UIView in question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.