[英]How to set autolayout constraint so that the height of an UIImageView is equal to the height of the superview?
I have created a UIView
that consists of 3 UILabel
s and 1 UIImageView
as below - 我创建了一个包含3个
UILabel
和1个UIImageView
的UIView
,如下所示-
The UILabel
s have lines
set to 0 so that the height of the UILabel
s change dynamically UILabel
的lines
设置为0,以便UILabel
的高度动态变化
I have added constraints such that the height of the image depends on the height of the UILabel
s as shown below - 我添加了一些约束,以使图像的高度取决于
UILabel
的高度,如下所示-
I have set width of the UIImageView
to a constant of 100. 我已将
UIImageView
宽度设置为常数100。
I would like to set a constraint on UIImageView
such that the height of the UIImageView
is equal to the height of the whole view. 我想在
UIImageView
上设置一个约束,以使UIImageView
的高度等于整个视图的高度。
I tried adding a "Equal Heights" constraint on the UIImageView
and the whole view. 我尝试在
UIImageView
和整个视图上添加“ Equal Heights”约束。 I observed that the height of the whole view depends on the height of the UIImageView
and not that the height of the UIImageView
depends on the whole view as I want it to be. 我观察到,整个视线的高度依赖于高度
UIImageView
,而不是该高度UIImageView
取决于整个视图,因为我希望它是。
Can anyone point out how I can add a constraint to the UIImageView
such that its height is equal to its superview but the height of the superview is not dependent on the UIImageView
? 谁能指出我如何向
UIImageView
添加约束,以使其高度等于其超级视图,但超级视图的高度不取决于UIImageView
?
Edit - The below image shows the problem that I am facing. 编辑-下图显示了我面临的问题。 When an image is set to the
UIImageView
, the height of the view changes. 将图像设置为
UIImageView
,视图的高度会更改。 I want the height of the UIImageView
to be equal to the height of the view, but I do not want the height of the view to be dependent on height of the UIImageView
我希望
UIImageView
的高度等于视图的高度,但是我不希望视图的高度取决于UIImageView
高度
I believe you haven't add the height constraint to the superview. 我相信您尚未将高度限制添加到超级视图中。 Setup height constraint for the superview and then add a "Equal Heights" constraint on the UIImageView and the whole view.
设置超级视图的高度约束,然后在UIImageView和整个视图上添加“等高”约束。
Looks like you need to reduce the vertical content compression resistance
on the image view. 看起来您需要减少图像视图上的
vertical content compression resistance
。
Set it to something very low like 100 and try again. 将其设置为非常低的值,例如100,然后重试。
In fact default compression resistance of your UIImageView
is 750, while content hugging of root UIView
is 250. This means AutoLayout engine layouts everything correctly. 实际上,
UIImageView
默认抗压缩性是750,而根UIView
内容包含是250。这意味着AutoLayout引擎可以正确地布局所有内容。 You can change vertical compression resistance to value lower then 250 ( UILayoutPriority.defaultLow - 1
) to achieve desired layout. 您可以将垂直压缩阻力更改为
UILayoutPriority.defaultLow - 1
250的值( UILayoutPriority.defaultLow - 1
)以实现所需的布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.