UICollectionView 单元格子视图不调整大小 [英] UICollectionView cell subviews do not resize
问题描述
在 CollectionView
中,一些单元格应该有一个额外的子视图或层.CollectionView
可以被告知调整其单元格的大小,因此所有内容都需要适当调整大小.
In a CollectionView
, some cells should have an additional subview or layer. The CollectionView
can be told to resize it's cells, thus all content needs to resize appropriately.
目前,该单元格是从一个包含带有 imageview 的单元格的 nib 初始化的;单元格笔尖链接到自定义 UICollectionViewCell
子类,该子类仅执行 init.Autoresize 子视图被选中.
Currently, the cell is initialized from a nib containing a cell with imageview; the cell nib is linked to a custom UICollectionViewCell
subclass, that only does the init. Autoresize subviews is checked.
CollectionView
被告知通过在 sizeForItemAtIndexPath:
中派生和返回的值来调整单元格的大小.我对 FlowLayout 进行了子类化,但它只指定 ScrollDirection
和 Insets
.
The CollectionView
is told to resize the cell by a value derived and returned in sizeForItemAtIndexPath:
. I have subclassed a FlowLayout but it only specifies ScrollDirection
and Insets
.
所有这些都运行良好.问题:如何将子视图/图层添加到单元格,以便它也正确调整大小?我尝试在关闭 translatesAutoresizingMaskIntoConstraints
的情况下添加子视图和图层,但它们根本不会自动更改大小.还尝试使用代码框架/视图而不是笔尖.
All of that is working fine. Problem: How do I add subview/layer to the cell so it also resizes correctly? I tried adding subviews and layers with translatesAutoresizingMaskIntoConstraints
off, but these do not automatically change size at all. Also tried to use code frame/view instead of nib.
我现在得到的最好的是一个 cell.contentView.layer
子层,我将它添加到 cellForItemAtIndexPath:
;这是通过从 sizeForItemAtIndexPath:
存储单元格的 frame.size
来手动"调整大小的,这不仅丑陋,而且最终导致子层对于不同的单元格具有不同的大小.
The best I got now is a cell.contentView.layer
sublayer which I add in cellForItemAtIndexPath:
; that is "manually" resized by storing the cell's frame.size
from sizeForItemAtIndexPath:
, which is not only ugly but also ends up with the sublayer having various sizes for different cells.
任何帮助表示赞赏!
推荐答案
解决方案是关闭单元格 xib 的 AutoConstraints 并激活图像视图的 AutoResize 中的灵活宽度/高度箭头.
The solution was to turn off AutoConstraints for the cell xib and activate the flexible width/height arrows in the AutoResize for the imageviews.
这篇关于UICollectionView 单元格子视图不调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!