且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有相对宽度/高度的iOS8自动版式

更新时间:2023-02-03 09:17:16

这是一种实现方法:

  1. 从上面列出的四个约束开始:中心X,中心Y,等宽(0.9乘数)和长宽比,它们的优先级均为1000.
  2. 将等宽"的优先级更改为750.这将使自动版面配置"在必要时忽略或修改此约束.
  3. 添加等高线"(视图-超级视图)约束,但将其乘以0.9使其小于或等于,而不是等于.保留其优先级为1000.

现在,当您处于纵向时,通过使红色框的宽度像以前一样为90%,自动版式"将能够满足您的所有限制.切换到横向时,等高"约束将确保红色框不会超过高度的90%,因为该约束的优先级为1000,但是自动布局"还会使框尽可能大,以尝试最适合等宽"约束.

Now, when you are in portrait, Auto Layout will be able to satisfy all of your constraints by making the width of the red box to be 90% as before. When you switch to landscape, the Equal Heights constraint will ensure that the red box doesn't grow beyond 90% of the height since this constraint has a 1000 priority, but Auto Layout will also make the box as big as possible in an attempt to best serve the Equal Widths constraint.