如何让使用自动布局,它的父方认为调整 [英] How to make a square view resize with its superview using auto layout

查看:187
本文介绍了如何让使用自动布局,它的父方认为调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的具体情况我有看法,对此我添加了以下限制视图控制器内的:

In my particular case I have view inside the view controller for which I added the following constraints:


  • 设置前置,顶部和底部边缘0

  • 乘数设为底边至2:1

现在的观点坐在视图控制器上的上半部分。

Now the view sits in the top half on the view controller.

这里面我添加了一个方形图像视图,为我增加了以下的限制:

Inside this I add a square image view, for which I added the following constraints:


  • 控制从图像视图中拖动到上海华并加入相等的高度和宽度。

  • 更改宽度和高度倍增,直到我有一个完美的正方形。

  • 添加的约束是中心垂直和水平

我的约束似乎完美,但在模拟器运行时,我没有得到一个完美的正方形。除此之外,图像视图没有得到不同的模拟器的屏幕上运行时调整。

My constraints seems perfect, but when running in the simulator I don't get a perfect square. Besides this, the image view doesn't get resize when running on different simulator screens.

这是我的设置:


  • 自动布局和大小的类启用

  • 我使用推断大小故事板

  • 自适应布局设置为任何宽度和高度

  • 我想为4S,5,6和6 +模拟器运行此。

我看了看其他计算器职位,但似乎没有任何工作。

I looked on other stackoverflow posts, but nothing seems to work.

是否有一些基本的步骤,这样做呢?

Are there some basic steps to do this ?

在这里输入的形象描述

编辑:

设置完成后> = 10约束:

After setting >=10 constraints:

在这里输入的形象描述

编辑3:我添加顶部,底部,导致尾随约束的2倍,1与低于或等于(优先1000),另一种具有大于或等于(优先权800)与90.我的恒定值不知道为什么,对于底部它试图从主视图,而不是容器视图(绿色的)口延伸到90磅。

edit 3: I added top,bottom,leading,trailing constraints 2 times, 1 with lower than or equal(priority 1000), the other one with greater than or equal(priority 800) with the constant value of 90. I don't know why for bottom it tries to streches to 90 pt from the main view, not the container view(green one).

在这里输入的形象描述

推荐答案

您有需要扩大,以填补其容器,同时保持其宽高比的看法。这是自动布局的通用模式。

You have a view that needs to expand to fill its container while maintaining its aspect ratio. This is a common pattern with Auto Layout.

诀窍是使用前/后/顶/底部的两个约束:

The trick is to use two constraints for leading/trailing/top/bottom:


  • = 10 以低优先级

方式> = 10 在需要优先

全部放在一起,你有:


  • 纵横比为1:1

  • Aspect Ratio 1:1

中心X / Y的SuperView中

Center X/Y in Superview

上升/下降/顶/底至上海华= 10(750优先级)

Leading/Trailing/Top/Bottom to Superview = 10 (at 750 priority)

上升/下降/顶/底到上海华> = 10(在1000的优先级)

Leading/Trailing/Top/Bottom to Superview >= 10 (at 1000 priority)

也有几件事情要考虑的UIImageView:

There are also a couple of things to consider with UIImageView:


  • 的UIImageView将它显示基于图像的固有内容的大小,所以你要确保其内容抱死优先级比你使用的750较低优先级 = 10 的约束。

UIImageView.contentMode 确定基础图像是如何大小相对的UIImageView的大小。默认情况下,它被设置为 UIViewContentModeScaleToFill

UIImageView.contentMode determines how the underlying image is sized relative to the size of the UIImageView. By default, it's set to UIViewContentModeScaleToFill.

这篇关于如何让使用自动布局,它的父方认为调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆