我正在尝试在UICollectionViewController
中设置iOS 8自动调整大小的单元格:
我使用StoryBoards创建了一个基本设计,并在那里设计了一个UICollectionViewCell,其标签约束在单元格的边界上。然后,我将该单元格的标签链接到Cell的类,以便能够以编程方式访问它
如果没有指定estimatedItemSize
,一切正常(当然除了自动调整大小)。
当我以这种方式指定estimatedItemSize
时:
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
layout.estimatedItemSize = CGSizeMake(100.0f, 30.0f);
}
应用程序因这些错误消息而崩溃:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item width must be less than the width of the UICollectionView minus the section insets left and right values.
Please check the values return by the delegate.
我认为,我将estimatedItemSize
设置在错误的位置,或者,如果我已经在故事板中设置了指定的项目大小,那么它与估计的大小有某种冲突。
答案 0 :(得分:4)
回答自己的问题,因为我发现了问题的原因:
要使此功能起作用,UILabel的宽度必须小于UICollectionView宽度。即使在Storyboard中看起来没问题,如果使用AutoLayout,也必须约束宽度,例如在我的情况下,我做了一个约束less or equal 320
。没有它,标签会增长到非常大的宽度,但只有1行高。应用此约束后,换行开始工作,标签长度增加而不是仅增加宽度。