简体   繁体   English

我应该使用什么纵横比NSLayoutConstraint?

[英]For what should I use the Aspect Ratio NSLayoutConstraint?

When I first started working with AutoLayout, I couldn't find any example which helps understanding for what stands the Aspect Ratio NSLayoutConstraint. 刚开始使用AutoLayout时,找不到任何有助于理解纵横比NSLayoutConstraint的示例。

Does someone have an example of usage? 有人有用法的例子吗? Thanks! 谢谢!

I have a demo example of keeping a square view centered in all devices and orientations here . 我有一个演示示例,在这里将正方形视图保持在所有设备和方向的中心。

The constraints are set up in IB, but you could just as easily set them up in code. 约束是在IB中设置的,但您也可以在代码中轻松设置它们。 Most of this was meant to explain how to prioritize margin constraints to let the auto layout engine know what to break and when, but what makes the view square is the aspect ration - it's 1:1. 其中大部分内容旨在说明如何对边距约束进行优先级排序,以使自动布局引擎知道要中断的内容和时间,但是使视角成正比的是宽高比 -1:1。 If you want a rectangle, you make it 2:1 (or 1:2 depending). 如果您想要一个矩形,则将其设为2:1(或取决于1:2)。

Once you understand these two pieces (prioritization and aspect ratio), the last piece is the actual margins values - I set them for 10 points, meaning the square will have 10 point margins on the smallest axis. 了解了这两部分(优先级和宽高比)后,最后一块是实际的边距值-我将它们设置为10点,这意味着正方形在最小轴上将有10点边距。 If the device is an iPad Pro 12.9 inch, you get a very large square. 如果该设备是一个iPad Pro的12.9英寸,你会得到一个非常大的广场。 If it's an iPhone SE, you get a very small one. 如果是iPhone SE,您将得到一个很小的手机。 No matter what, you get the 10 point margin. 无论如何,您将获得10点的保证金。 Set the margin values up for 50, and the square view is relatively smaller. 将边距值设置为50,并且正方形视图相对较小。 The important thing is with an aspect ratio of 1:1, it will always be a square. 重要的是长宽比为1:1,它将始终是正方形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM