如何根据swift 3中的Collection View Content Height调整UIView高度? [英] How to adjust UIView height according to the Collection View Content Height in swift 3?
问题描述
我在项目中调整 UIView 高度时遇到问题.我说明了这个项目.
I have a problem of adjusting the UIView height in my project. I illustrated the project.
让我解释一下我的项目图片.
每个 Orange UIView 都有 trailing、leading、top 和 bottom 约束 和 垂直间距 到每个 UIView.
但是,我向每个 UIView 添加高度",因为如果我不添加,它会给出错误并添加高度"警告.
在 UIView2 中,我添加了 3 个容器视图.每个容器视图有 3 个视图控制器.(数据 1 视图、数据 2 视图、数据 3 视图)
我在每个视图中嵌入了集合视图.
问题是在 Data 3 View 中有很多集合 View 单元格数据.因此,Orange UIView 的高度应该根据集合视图的内容增加.
目前,如果用户单击数据 3 分段控制按钮,我只需添加固定高度常量(高度:800).
但是,我不想像这样设置固定高度.
自上周以来,我一直在尝试解决这个问题.直到现在我都无法解决这个问题.
谁能帮我逐步解释一下吗?因为我不知道我该怎么做.
Let me explain about my project image.
Each Orange UIView has trailing, leading , top and bottom constraints and vertical spacing to each UIView.
But, I add "Height" to each UIView because if I don't add, it gives error and add the "Height" warning.
In UIView2, I add 3 Container Views. Each Container Views has 3 View Controllers. (Data 1 View, Data 2 View, Data 3 View)
I embed Collection View inside in each views.
The problem is that in Data 3 View has many collection View cell data. So, the height of Orange UIView should be increase according to the collection view content.
Currently, I just add fixed height constant (height:800) if users click on the Data 3 segmented control button.
But, I don't want to set fixed height like this.
I've been trying to solve this problem since last week. I could not solve this till now.
Can anyone help me explain in step by step please? Because I have no idea of what should I do.
推荐答案
由于 UITableView
或 UICollectionView
继承自 UIScrollView
他们基本上没有intrinsicContentSize
.
您有两种方法可以使高度与内容大小(可以滚动的空间)相同:
Since UITableView
or UICollectionView
inherit from UIScrollView
they basically do not have an intrinsicContentSize
.
You have two ways to make the same height as the content size (the space that you can scroll):
- 您可以继承集合视图并重写
intrinsicContentSize
方法以返回contentSize
和sizeThatFits:
方法 - 在界面中创建一个高度约束,并将其设置为等于集合视图的内容大小.
- You can subclass the collection view and override
intrinsicContentSize
method to return thecontentSize
and thesizeThatFits:
method as well - Create a height constraint in the interface and set it as equal to the content size of the collection view.
这篇关于如何根据swift 3中的Collection View Content Height调整UIView高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!