Reputation: 3795
I have a requirement to layout an array of photos as shown. The code to generate the required layout is as follow where UICollectionViewFlowLayout
is a super class of PhotoLayoutReordering
:
@implementation PhotoLayoutReordering
-(instancetype) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
;// additional set up here
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return self;
}
-(CGSize) calculateSecondaryCellSizeAndMargin {
CGSize cellSize;
cellSize.width = self.collectionView.frame.size.width/NUMBER_OF_COLUMNS;
cellSize.width = cellSize.width - 2*SPACE_BETWEEN_CELLS;
CGSize secondaryCellSize = CGSizeMake(cellSize.width,cellSize.width);
// NSLog(@"cell width:%1.0f\t cell height:%1.0f\t margin:%1.0f\n",secondaryCellSize.width,secondaryCellSize.height,_spaceBetweenCells);
// NSLog(@"number of cells are: %d",(int)[self.collectionView numberOfItemsInSection:0]);
return secondaryCellSize;
}
-(CGSize) calculatePrimaryCellSize {
CGFloat width;
CGSize secondaryCellSize = [self calculateSecondaryCellSizeAndMargin];
width = (NUMBER_OF_COLUMNS-1)* secondaryCellSize.width + NUMBER_OF_COLUMNS*SPACE_BETWEEN_CELLS;
return CGSizeMake(width, width);
}
-(UICollectionViewLayoutAttributes*) layoutAttributesAtIndexPath:(NSIndexPath*) indexPath {
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
return attributes;
}
#pragma mark - UICollectionViewLayout overridden methods
-(CGSize) collectionViewContentSize {
CGSize primaryCellSize = [self calculatePrimaryCellSize];
CGFloat heightForPrimaryCell = primaryCellSize.height + NUMBER_OF_COLUMNS*SPACE_BETWEEN_CELLS;
if ([self.collectionView numberOfItemsInSection:0] > NUMBER_OF_COLUMNS) {
NSUInteger nubmerOfSecondaryRows = [self.collectionView numberOfItemsInSection:0]/NUMBER_OF_COLUMNS;
CGFloat heightForSecondaryCells = nubmerOfSecondaryRows * [self calculateSecondaryCellSizeAndMargin].height;
heightForPrimaryCell += heightForSecondaryCells;
}
// NSLog(@"height of collectionview %1.0f",heightForPrimaryCell);
return CGSizeMake(self.collectionView.frame.size.width, heightForPrimaryCell);
}
-(NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
NSLog(@"layoutAttributesForElementsInRect");
NSMutableArray *attributesInRect = [NSMutableArray array];
NSInteger numberOfCell = [self.collectionView numberOfItemsInSection:0];
for (int index=0; index < numberOfCell; index++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[attributesInRect addObject:[self calculateLayouAttributes:indexPath]];
}
for (UICollectionViewLayoutAttributes *attribute in attributesInRect) {
NSLog(@"%@",attribute);
}
return attributesInRect;
}
-(UICollectionViewLayoutAttributes*) layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"layoutAttributesForItemAtIndexPath");
return [self calculateLayouAttributes:indexPath];
}
I am using Xcode6. When running the simulator with iOS8, the layout is correct for iPhone4s, iPhone5, iPhone6, and iPhone6 Plus. The array of attributes returned from -(NSArray*) layoutAttributesForElementsInRect:
shown below is correct.
2014-09-16 09:47:30.600 Postlets[3606:212466] layoutAttributesForElementsInRect
2014-09-16 09:47:30.600 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1880> index path: (<NSIndexPath: 0x7afb0dc0> {length = 2, path = 0 - 0}); frame = (2 2; 236 236);
2014-09-16 09:47:30.600 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1950> index path: (<NSIndexPath: 0x7afb1900> {length = 2, path = 0 - 1}); frame = (242 2; 76 76);
2014-09-16 09:47:30.600 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb19d0> index path: (<NSIndexPath: 0x7afb1910> {length = 2, path = 0 - 2}); frame = (242 82; 76 76);
2014-09-16 09:47:30.600 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1a60> index path: (<NSIndexPath: 0x7afb1a50> {length = 2, path = 0 - 3}); frame = (242 162; 76 76);
2014-09-16 09:47:30.600 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1af0> index path: (<NSIndexPath: 0x7afb1ae0> {length = 2, path = 0 - 4}); frame = (2 242; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1b90> index path: (<NSIndexPath: 0x7afb0be0> {length = 2, path = 0 - 5}); frame = (82 242; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1c20> index path: (<NSIndexPath: 0x7afb1c10> {length = 2, path = 0 - 6}); frame = (162 242; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1ca0> index path: (<NSIndexPath: 0x7afb1920> {length = 2, path = 0 - 7}); frame = (242 242; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1d20> index path: (<NSIndexPath: 0x7afb1930> {length = 2, path = 0 - 8}); frame = (2 322; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1de0> index path: (<NSIndexPath: 0x7afb1940> {length = 2, path = 0 - 9}); frame = (82 322; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1e70> index path: (<NSIndexPath: 0x7afb1e60> {length = 2, path = 0 - 10}); frame = (162 322; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1f00> index path: (<NSIndexPath: 0x7afb1ef0> {length = 2, path = 0 - 11}); frame = (242 322; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb1f90> index path: (<NSIndexPath: 0x7afb1f80> {length = 2, path = 0 - 12}); frame = (2 402; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb2020> index path: (<NSIndexPath: 0x7afb2010> {length = 2, path = 0 - 13}); frame = (82 402; 76 76);
2014-09-16 09:47:30.601 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb20b0> index path: (<NSIndexPath: 0x7afb20a0> {length = 2, path = 0 - 14}); frame = (162 402; 76 76);
2014-09-16 09:47:30.602 Postlets[3606:212466] <UICollectionViewLayoutAttributes: 0x7afb2140> index path: (<NSIndexPath: 0x7afb2130> {length = 2, path = 0 - 15}); frame = (242 402; 76 76);
However, when running against iOS7.1, the layout for the first cell is incorrect for iPhone4s/iPhone5.
I am not sure if I overlooked something or if there's a bug with UICollectionViewFlowLayout? Thanks for your input.
UPDATE: The subclass of UICollectionViewCell
has a UIImageView
. In the storyboard, I am using autolayout to constrain the size of UIImageView
to be flushed to the subclass of UICollectionViewCell
on all 4 sides. The interpretation of the constraints in Xcode6 targeting iOS7 is misinterpreted somehow (at least that's a guess on my part.) I decided to use the Visual Format Language to add the constraints as shown below:
-(void) layoutViews {
_imageView = [[RemoteImageView alloc] init]; // this is a subclass of UIImageView
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.clipsToBounds = YES;
[self addSubview:_imageView];
NSDictionary *views = NSDictionaryOfVariableBindings(_imageView);//,spinner);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_imageView]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView]|" options:0 metrics:nil views:views]];
}
The UIImageView
now is properly sized to be flushed with the UICollectionViewCell
on all 4 sides for iOS7/8. Hope this is useful to some developers out there...
Filed bug report with Apple (#18415152).
UPDATE 10/7/14: Closed by Apple (duplicate of bug #18312246)
Upvotes: 1
Views: 1056
Reputation: 4446
Where are you setting the size of the cells? It looks like you're missing the collectionView:layout:sizeForItemAtIndexPath:
delegate method. This is where you set the size of an individual cell.
Upvotes: 1