简体   繁体   English

如何创建高度取决于子视图的视图?

[英]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.

相关问题 如何根据子视图内容使视图高度增长 - How to make a view height grow depending on its child views contents 视图高度包装子视图内容 - View height wrap child views content 如何在UITableViewCell中创建具有动态高度的视图网格 - How to create a grid of views with dynamic height in UITableViewCell iOS Autolayout - 如何在视图之间设置两个不同的距离,取决于屏幕高度 - iOS Autolayout - How to set two different distances between views, depends on the screen height 动态视图的高度取决于表格视图的高度? - Dynamic view's height depends on table view's height? 自动布局:设置高度视图取决于其他高度视图 - Auto Layout: Set height View depends on other height View 如何以父视图大小的百分比设置视图的高度 - How to set height of the views in percentage of parent view size 如何将固定高度视图添加到垂直堆栈视图? - How do I add fixed height views to vertical stack view? 如何使用一系列视图中具有最高高度的视图进行约束 - How to make a constraint use the view with highest height from an array of views 如何在固定高度的特定视图中 append 子视图 controller - how to append child view controller in specific view with fixed height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM