[英]How to create a View that height depends on child views?
This is the same as asking how to achieve wrap_content
in iOS. 这与询问如何在iOS中实现
wrap_content
相同。
However, I found no suitable solution. 但是,我找不到合适的解决方案。 Almost all solutions suggest to override
intrinsicContentSize()
. 几乎所有解决方案都建议覆盖
intrinsicContentSize()
。 What if the view does not know any information about its child's height? 如果视图不知道有关其孩子身高的任何信息怎么办?
The case is like this: 案件是这样的:
I want to create a BaseDialogViewController that is responsible for blurring the background and displaying a dialog container. 我想创建一个BaseDialogViewController,它负责模糊背景并显示一个对话框容器。 Then if I want to implement a dialog, I will extends BaseDialogViewController and add the dialog content to the container view.
然后,如果我想实现一个对话框,我将扩展BaseDialogViewController并将对话框内容添加到容器视图中。
So, the container needs to adjust its height according to the total height of the content inside of it. 因此,容器需要根据其内部的内容的总高度来调整其高度。
How do I achieve this? 我该如何实现这一目标?
Just set constraints for all borders from the child view to the container view. 只需为子视图到容器视图设置所有边框的约束。 You should ensure that the child view can compute its size from its contained elements, by setting constrains for all borders to its child elements, and so on.
您应该确保子视图可以通过为其子元素设置所有边框的约束来计算其包含元素的大小,等等。
The container view should only have additional constraints for its position (eg centering in its parent view). 容器视图应该只对其位置有其他约束(例如,在其父视图中居中)。
The rest is done by Autolayout. 其余的由Autolayout完成。 It will calculate the size from the innermost elements with fixed or intrinsic sizes (eg labels, text fields, buttons) to the outer elements.
它将计算从具有固定或固有尺寸(例如标签,文本字段,按钮)的最内层元素到外部元素的大小。
I realized that if child views height is specified, or has an intrinsic height, 我意识到如果指定了儿童视图高度,或者具有固有高度,
you can set constraint from parent's bottom to child's bottom , and by setting this, the parent's height will be able to vary according to the bottom position of child's view. 你可以设置从父母的底部到孩子的底部的约束,通过设置它,父母的身高将根据孩子视图的底部位置而变化。
I found this out from this answer . 我从这个答案中找到了这个 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.