UICollectionView cell subviews do not resize UICollectionView cell subviews do not resize ios ios

UICollectionView cell subviews do not resize


I ran into the same issue just now.

When using the UICollectionViewFlowLayoutDelegate method to set the cell size depending on device and device-orientation, the size would be calculated properly but subviews would not resize to fill the newly size cell. The effect was a large blank cell with small subviews that don't fill the cell's bounds / remain the size equal to their current value in the xib file.

I solved this by doing the following in awakeFromNib:

- (void)awakeFromNib{    [super awakeFromNib];    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;    self.contentView.translatesAutoresizingMaskIntoConstraints = YES;}

Prior to doing this, the contentView mask was nil.


*override func awakeFromNib() {    super.awakeFromNib()    self.contentView.autoresizingMask.insert(.FlexibleHeight)    self.contentView.autoresizingMask.insert(.FlexibleWidth)}

This worked for me.. This code goes inside of your subclassed UICollectionViewCell.swift file (where your code involving the custom cell is located)

Swift solution*


As an alternative to enabling AutoResizingMask, for custom UICollectionViewLayouts that have variable height for example where you are setting some constraints manually and need translatesAutoresizingMaskIntoConstraints to remain NO, you can add the following to layoutSubviews in the cell:

self.contentView.frame = self.bounds;

This worked to fix all of my custom collection view layouts that had problens with Xcode 6.