如何将 UICollectionView 的高度调整为 UICollectionView 内容大小的高度? [英] How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?

查看:26
本文介绍了如何将 UICollectionView 的高度调整为 UICollectionView 内容大小的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

覆盖 func layoutSubviews() {super.layoutSubviews()如果 !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {self.invalidateIntrinsicContentSize()}}覆盖 var intrinsicContentSize: CGSize {返回 self.collection.contentSize}

return self.collection.contentSize 总是返回 (width, 0)并且由于这个原因,它缩小到高度 30 的值(我在 XIB 文件中为高度设置的值,尽管我有约束 >= 30).

解决方案

我建议如下:

<块引用>

  1. 为您的集合视图添加高度限制.
  2. 将其优先级设置为 999.
  3. 将其常量设置为使其在情节提要上合理可见的任何值.
  4. 将集合视图的底部相等约束更改为大于或等于.
  5. 将高度限制连接到插座.
  6. 每次在集合视图上重新加载数据时,请执行以下操作:

您可能还想通过将集合视图的 Inset 添加到内容大小来考虑它.

代码示例:

CGFloat height = myCollectionView.collectionViewLayout.collectionViewContentSize.heightheightConstraint.constant = 高度self.view.setNeedsLayout() 或 self.view.layoutIfNeeded()

说明:额外,看懂了就不用看了.很明显!!

无论优先级如何,UI 都会尝试反映所有约束.由于存在 (999) 优先级较低的高度约束,以及大于或等于类型的底部约束.每当高度约束常量设置为小于父视图高度的值时,集合视图将等于给定高度,实现这两个约束.

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

以下只是经验的猜测.因此,它实现了一个常数.但是,它也会尝试使结果 UI 中的错误为其他未实现的较低优先级约束尽可能低.因此,集合视图高度将等于父视图大小.

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:

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

解决方案

I would suggest the following:

  1. Add a height constraint to your collection view.
  2. Set its priority to 999.
  3. Set its constant to any value that makes it reasonably visible on the storyboard.
  4. Change the bottom equal constraint of the collection view to greater or equal.
  5. Connect the height constraint to an outlet.
  6. Every time you reload the data on the collection view do the following:

You may also want to consider the Inset of the collection view by adding it to the content size.

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 there is a height constraint with 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.

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.

这篇关于如何将 UICollectionView 的高度调整为 UICollectionView 内容大小的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆