如何将UICollectionView的高度调整为UICollectionView的内容大小的高度?

时间:2022-09-25 18:30:47

I would like the UICollectionView (the red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of empty space. What I tried is to use:

我想UICollectionView(红色的)缩小到内容大小的高度,在这种情况下UICollectionViewCells(黄色的)因为有很多空的空间。我尝试使用的是:

override func layoutSubviews() {
    super.layoutSubviews()
    if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
        self.invalidateIntrinsicContentSize()
    }
}

override var intrinsicContentSize: CGSize {
    return self.collection.contentSize
}

but return self.collection.contentSize always return (width, 0) and for this reason it shrinks too much to value of height 30 (the value which I set in the xib file for the height, although I have constaint >= 30).

但是返回self.collection.contentSize总是返回(width,0),因此它会收缩到高度为30的值(我在xib文件中为高度设置的值,尽管我有constaint> = 30)。

如何将UICollectionView的高度调整为UICollectionView的内容大小的高度?

5 个解决方案

#1


59  

I would suggest the following:

我建议如下:

  1. Add a height constraint to your collection view.
  2. 向集合视图添加高度约束。
  3. Set its priority to 999.
  4. 将其优先级设置为999。
  5. Set its constant to any value that only makes it reasonably visible on the storyboard.
  6. 将其常量设置为只在故事板上合理显示的任何值。
  7. Change the bottom equal constraint of the collection view to greater or equal.
  8. 将集合视图的底部相等约束更改为更大或相等。
  9. Connect the height constraint to an outlet.
  10. 将高度约束连接到插座。
  11. Every time you reload the data on the collection view do the following:
  12. 每次在集合视图上重新加载数据时,请执行以下操作:

Code Sample:

代码示例:

CGFloat height = myCollectionView.collectionViewLayout.collectionViewContentSize.height
heightConstraint.constant = height
self.view.setNeedsLayout() Or self.view.layoutIfNeeded()

Explanation: Extra, You don't have to read if you understand it. obviously!!

说明:额外,如果您理解,则无需阅读。明显!!

The UI will try to reflect all the constraints no matter what are their priorities. Since the height constraint has lower priority of (999), and a bottom constraint of type greater or equal. whenever, the height constraint constant is set to a value less than the parent view height the collection view will be equal to the given height, achieving both constraints.

用户界面将尝试反映所有约束,无论其优先级如何。由于高度约束的优先级较低(999),而底部约束的类型大于或等于。每当高度约束常量设置为小于父视图高度的值时,集合视图将等于给定高度,从而实现两个约束。

But, when the height constraint constant set to a value more than the parent view height both constraints can't be achieved. Therefore, only the constraint with the higher priority will be achieved which is the greater or equal bottom constraint.

但是,当高度约束常量设置为大于父视图高度的值时,两个约束都无法实现。因此,只有具有较高优先级的约束才能实现,即较大或相等的底部约束。

The following is just a guess from an experience. So, it achieves one constrant. But, it also tries to make the error in the resulted UI for the other un-achieved lower priority constraint as lowest as possible. Therefore, the collection view height will be equal to the parent view size.

以下只是一种体验的猜测。因此,它实现了一个实例。但是,它还尝试使得结果UI中的错误尽可能地低于其他未实现的较低优先级约束。因此,集合视图高度将等于父视图大小。

#2


4  

You have to set height constraint as equal to content size

您必须将高度约束设置为等于内容大小

HeightConstraint.constant = collection.contentSize.height 

#3


2  

Do following.

做以下。

  1. first set height constrain for UICollectionView
  2. 首先为UICollectionView设置高度约束
  3. here calendarBaseViewHeight is UICollectionView height Variable
  4. 这里calendarBaseViewHeight是UICollectionView高度变量
  5. call the function after reload the collection view

    重新加载集合视图后调用该函数

    func resizeCollectionViewSize(){  
           calendarBaseViewHeight.constant = collectionView.contentSize.height      
     }
    

#4


2  

I ended up, by subclassing the UICollectionView and overriding some methods as follows.

我最后通过继承UICollectionView并覆盖一些方法,如下所示。

  • Returning self.collectionViewLayout.collectionViewContentSize for intrinsicContentSize makes sure, to always have the correct size
  • 为intrinsicContentSize返回self.collectionViewLayout.collectionViewContentSize可确保始终具有正确的大小
  • Then just call it whenever it might change (like on reloadData)
  • 然后只要它可能改变就调用它(就像在reloadData上一样)

Code:

码:

override func reloadData() {
    super.reloadData()
    self.invalidateIntrinsicContentSize()
}

override var intrinsicContentSize: CGSize {
    return self.collectionViewLayout.collectionViewContentSize
}

#5


1  

first of all calculate number of cells than multiply it with height of cell and then return height in this method

首先计算细胞数,然后将细胞数乘以细胞高度,然后在此方法中返回高度

    collectionView.frame = CGRectMake (x,y,w,collectionView.collectionViewLayout.collectionViewContentSize.height); //objective c
    //[collectionView reloadData];
   collectionView.frame = CGRect(x: 0, y: 0, width: width, height: collectionView.collectionViewLayout.collectionViewContentSize.height) // swift

#1


59  

I would suggest the following:

我建议如下:

  1. Add a height constraint to your collection view.
  2. 向集合视图添加高度约束。
  3. Set its priority to 999.
  4. 将其优先级设置为999。
  5. Set its constant to any value that only makes it reasonably visible on the storyboard.
  6. 将其常量设置为只在故事板上合理显示的任何值。
  7. Change the bottom equal constraint of the collection view to greater or equal.
  8. 将集合视图的底部相等约束更改为更大或相等。
  9. Connect the height constraint to an outlet.
  10. 将高度约束连接到插座。
  11. Every time you reload the data on the collection view do the following:
  12. 每次在集合视图上重新加载数据时,请执行以下操作:

Code Sample:

代码示例:

CGFloat height = myCollectionView.collectionViewLayout.collectionViewContentSize.height
heightConstraint.constant = height
self.view.setNeedsLayout() Or self.view.layoutIfNeeded()

Explanation: Extra, You don't have to read if you understand it. obviously!!

说明:额外,如果您理解,则无需阅读。明显!!

The UI will try to reflect all the constraints no matter what are their priorities. Since the height constraint has lower priority of (999), and a bottom constraint of type greater or equal. whenever, the height constraint constant is set to a value less than the parent view height the collection view will be equal to the given height, achieving both constraints.

用户界面将尝试反映所有约束,无论其优先级如何。由于高度约束的优先级较低(999),而底部约束的类型大于或等于。每当高度约束常量设置为小于父视图高度的值时,集合视图将等于给定高度,从而实现两个约束。

But, when the height constraint constant set to a value more than the parent view height both constraints can't be achieved. Therefore, only the constraint with the higher priority will be achieved which is the greater or equal bottom constraint.

但是,当高度约束常量设置为大于父视图高度的值时,两个约束都无法实现。因此,只有具有较高优先级的约束才能实现,即较大或相等的底部约束。

The following is just a guess from an experience. So, it achieves one constrant. But, it also tries to make the error in the resulted UI for the other un-achieved lower priority constraint as lowest as possible. Therefore, the collection view height will be equal to the parent view size.

以下只是一种体验的猜测。因此,它实现了一个实例。但是,它还尝试使得结果UI中的错误尽可能地低于其他未实现的较低优先级约束。因此,集合视图高度将等于父视图大小。

#2


4  

You have to set height constraint as equal to content size

您必须将高度约束设置为等于内容大小

HeightConstraint.constant = collection.contentSize.height 

#3


2  

Do following.

做以下。

  1. first set height constrain for UICollectionView
  2. 首先为UICollectionView设置高度约束
  3. here calendarBaseViewHeight is UICollectionView height Variable
  4. 这里calendarBaseViewHeight是UICollectionView高度变量
  5. call the function after reload the collection view

    重新加载集合视图后调用该函数

    func resizeCollectionViewSize(){  
           calendarBaseViewHeight.constant = collectionView.contentSize.height      
     }
    

#4


2  

I ended up, by subclassing the UICollectionView and overriding some methods as follows.

我最后通过继承UICollectionView并覆盖一些方法,如下所示。

  • Returning self.collectionViewLayout.collectionViewContentSize for intrinsicContentSize makes sure, to always have the correct size
  • 为intrinsicContentSize返回self.collectionViewLayout.collectionViewContentSize可确保始终具有正确的大小
  • Then just call it whenever it might change (like on reloadData)
  • 然后只要它可能改变就调用它(就像在reloadData上一样)

Code:

码:

override func reloadData() {
    super.reloadData()
    self.invalidateIntrinsicContentSize()
}

override var intrinsicContentSize: CGSize {
    return self.collectionViewLayout.collectionViewContentSize
}

#5


1  

first of all calculate number of cells than multiply it with height of cell and then return height in this method

首先计算细胞数,然后将细胞数乘以细胞高度,然后在此方法中返回高度

    collectionView.frame = CGRectMake (x,y,w,collectionView.collectionViewLayout.collectionViewContentSize.height); //objective c
    //[collectionView reloadData];
   collectionView.frame = CGRect(x: 0, y: 0, width: width, height: collectionView.collectionViewLayout.collectionViewContentSize.height) // swift