-->

How to adjust UIView height according to the Colle

2019-01-29 11:54发布

问题:

I have a problem of adjusting the UIView height in my project. I illustrated the project.

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.

回答1:

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):

  • You can subclass the collection view and override intrinsicContentSize method to return the contentSize and the sizeThatFits: method as well
  • Create a height constraint in the interface and set it as equal to the content size of the collection view.