具有相对宽度/高度的iOS8自动版式 [英] IOS8 Autolayout with relative width / height
问题描述
我需要使用单个UIControl自动布置情节提要UIView. UIControl应该居中,长宽比应为1:1,并且应始终使用90%的超级视图宽度或高度,具体取决于设备的方向,而不会发生剪切.
I need to autolayout a storyboard UIView with a single UIControl. The UIControl should be centered, needs to have a aspect ratio of 1:1 and should always use 90% of the superviews width or height depending on the device orientation without clipping.
我添加了4个约束
- 中心Y对齐-视图-超级视图
- 中心X对齐-视图-超级视图
- 等宽-视图-乘数设置为0.9的超级视图
- 纵横比-视图-乘数设置为1:1的视图
此设置在纵向模式下工作正常,但在横向模式下失败. (请参见下面的图片)
This setup works fine in portrait mode but fails in landscape mode. (See pictures below)
我需要哪种约束来解决此问题.如果设备旋转了,我是否需要更改约束条件?
What kind of constraint do i need to solve this problem. Do i have to change the constraints if the device is rotated?
人像模式
风景模式-错误
横向模式-样机.这是我想要的.
Landscape mode - mockup. Thats what i want.
推荐答案
这是一种实现方法:
- 从上面列出的四个约束开始:中心X,中心Y,等宽(0.9乘数)和长宽比,它们的优先级均为1000.
- 将等宽"的优先级更改为750.这将使自动版面配置"在必要时忽略或修改此约束.
- 添加等高线"(视图-超级视图)约束,但将其乘以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.
这篇关于具有相对宽度/高度的iOS8自动版式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!