What is the purpose of the NSLayoutConstraint "Aspect Ratio"?

2

Hello,

Since I started working with AutoLayout, I did not find anything that explained how / to% As_% Aspect Ratio works. Even in the Apple documentation I did not find anything that would help to understand.

Does anyone have an example to share?

    
asked by anonymous 24.03.2017 / 11:40

1 answer

1

I ended up asking the question in English, the answer was quite helpful:

UPDATE

Explanation in Portuguese, translated from the given answer:

  

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

     

Constants are configured in the IB, but you can also do so by the code. Anyway, the goal here is to explain how to prioritize margin constants to make AutoLayout know what it should break and when. What holds the square as it stands is its aspect ratio - it is 1: 1. If it were 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 a smaller square, regardless of the situation, you will have a margin of 10 points. but the square is going to be relatively small. Anyway here, the important thing is that you always have a ratio of 1: 1, this view will always be a square ..

Conclusion

As% Aspect Ratio is the proportion constant of the NSLayoutConstraint in question.

    
31.03.2017 / 04:45