简体   繁体   English

如何设置自动布局约束,以使UIImageView的高度等于超级视图的高度?

[英]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个UIImageViewUIView ,如下所示-

视图

The UILabel s have lines set to 0 so that the height of the UILabel s change dynamically UILabellines设置为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.

相关问题 如何使用自动布局动态更改超级视图的高度 - How to dynamically change superview's height with autolayout 以编程方式使用自动布局的宽度和高度等于其超级视图? - Width and Height Equal to its superView using autolayout programmatically? iOS自动布局:给顶部空间等于超级视图高度的1/5 - iOS Autolayout : Give top space equal to 1/5 of superview height 根据子视图高度约束设置超视图高度约束 - set superview height constraint according to subview height constraint 视图和超级视图自动布局的比例高度 - proportional height of view and superview autolayout 添加等于Superview高度倍数的垂直空间约束 - Adding vertical space constraint equal to superview height multiple 如何使用UIButton设置UIimageView的AutoLayout约束? - How to set AutoLayout constraint of UIimageView with UIButton? 自动布局-使超级视图的高度等于子视图的高度+恒定,其中子视图为textView - Autolayout - Make superview height equal to subview height + constant where subview is textView 如何以编程方式调整相等高度的AutoLayout乘数? - How to adjust equal height AutoLayout multiplier programatically? 如何在 SwiftUI 中按比例设置高度和宽度? - How to set height and width in proportion with superview in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM