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
I had the same problem. Switching between two layouts did not resize the Pictures (UIImage) inside my cells. My Cells where build without a xib. And I used two different cell classes for each CollectionViewCustomLayout.
I fixed this programatically with this:
self.autoresizesSubviews = YES;
in my UICollectionViewCell subclasses.
But this only worked for me by adding the Picture as a cells backgroundpicture like this:
cell.backgroundView[[UIImageView alloc] initWithImage: SumDummyImage ];
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.
In another project without xibs i subclassed UICollectionViewCell and did this for the same effect:
#import "CVcell.h"
@implementation CVcell
@synthesize cellImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGFloat cellSize = self.contentView.bounds.size.width;
cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cellSize, cellSize)];
[cellImage setClipsToBounds:YES];
cellImage.translatesAutoresizingMaskIntoConstraints = NO;
cellImage.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.contentView addSubview:cellImage];
}
return self;
}
@end
I always prefer autolayout when possible. But Sometimes usings frames and bounds just is a timesaver when a view is determined straightforward by only its superview.
In the case of UICollectionViewCell I set an image to be the cells frame +
self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
But when I had different sizes of cells in the collectionView it messed up things and the image sometimes took the size of a different cell.
So I turned to working with the the cells bounds and - ye that acually worked out fine.
So maybe give that a try?
I'm adding subView and constraint programmatically, the following code works for me:
lazy var imageView: UIImageView = { [unowned self] in
let imageView = UIImageView(frame: self.contentView.frame)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
return imageView
}()
func updateCellWith(image: UIImage) {
contentView.subviews.forEach { $0.removeFromSuperview() }
contentView.addSubview(imageView)
imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
imageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
self.imageView.autoresizingMask.insert(.flexibleHeight)
self.imageView.autoresizingMask.insert(.flexibleWidth)
imageView.image = image
}
The solution was to turn off AutoConstraints for the cell xib and activate the flexible width/height arrows in the AutoResize for the imageviews.