xcode 自动布局:需要 y 位置或高度的约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47634284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:27:49  来源:igfitidea点击:

Autolayout : Need constraints for y position or height

iosxcodeautolayoutios-autolayout

提问by Gaurav Pandey

I am trying to satisfying the constraint form a long time but getting above error i.e. is "Need constraints for y position or height" for Yellow View and Green View. Basically I have 2 UILabel (dynamic height) within UIView and UIImageView of 4:3 ratio within a Cell.

我试图长时间满足约束形式,但遇到错误,即黄色视图和绿色视图的“ y 位置或高度需要约束”。基本上我在一个单元格内的 UIView 和 UIImageView 中有 2 个 UILabel(动态高度),比例为 4:3。

enter image description here

在此处输入图片说明

And my applied constraint is

我应用的约束是

enter image description here

在此处输入图片说明

I am also including sample project herewith above issue.

我还在此处包含了具有上述问题的示例项目。

回答by Prashant Tukadiya

Just one Constraints required

只需要一个约束

enter image description here

在此处输入图片说明

Provide SubHeading label height>= (Greater than equal to) Constraints

提供副标题标签height>= ( Greater than equal to) 约束

Because of you have applied aspect ratio of UIImageViewof 4:3 so it required to fixed other items height and Y position before it apply a ratio to it. By giving height to subheading Autolayout is able to calculate approx value min required in image ratio

由于您应用UIImageView了 4:3 的纵横比,因此在对其应用比例之前需要固定其他项目的高度和 Y 位置。通过为副标题提供高度,Autolayout 能够计算出图像比例所需的近似值 min

Hope it is helpful to you

希望对你有帮助

回答by sRoy

I am attaching two images, one is output and another one is the constraints of views (UILabel, UIImageView). For testing purpose, I put long texts in UILabel, after testing I reduced the text to 'Heading it is' and 'Sub heading it is'. Let me know, if its working of not.enter image description here

我附上两张图片,一张是输出,另一张是视图的约束(UILabel、UIImageView)。出于测试目的,我将长文本放在 UILabel 中,测试后我将文本缩减为“标题是”和“子标题是”。让我知道,如果它不起作用。请在此处输入图像描述

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明