UICollectionView单元子视图不会调整大小 [英] UICollectionView cell subviews do not resize

查看:305
本文介绍了UICollectionView单元子视图不会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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。检查自动调整子视图。

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关闭添加子视图和图层,但这些根本不会自动更改大小。还尝试使用代码框架/视图而不是nib。

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屋!

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