如何根据Swift 3中的“收藏夹视图内容高度"调整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的每个UIView都有跟踪,前导,顶部和底部约束和垂直间距.
但是,我将"Height"添加到每个UIView,因为如果不添加,它将给出错误并添加"Height"警告.
在UIView2中,我添加了3个容器视图.每个容器视图都有3个视图控制器. (数据1视图,数据2视图,数据3视图)
我在每个视图中都嵌入了集合视图.
问题在于,在数据3"视图中,有许多集合视图单元格数据.因此,应根据集合视图的内容增加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中的“收藏夹视图内容高度"调整UIView高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!