I've an array of UIColor's in my data source, what I'm trying to do is to fit all cells' size so they can all be seen on screen without scrolling.

I don't know how to do it, so far I've:

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    // Configure the cell
    UIColor *cellColor = (UIColor *)self.dataSource[indexPath.row];
    cell.backgroundColor = cellColor;

    for (UIView *view in cell.contentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            if ([[(UIImageView *)view image] isEqual:[UIImage imageNamed:@"V_BG.png"]]) {
                [view removeFromSuperview];
            }
        }
    }

    if ([cellColor isEqual:self.existingColor]) {
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"V_BG.png"]];
        img.frame = CGRectMake(0, 0, 30, 30);
        img.center = cell.contentView.center;
        img.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:img];
    }

    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{    
    [[NSNotificationCenter defaultCenter] postNotificationName:UIColorDidChangeNotification
                                                        object:[NSString stringWithFormat:@"%ld", (long)indexPath.row]];

    [self.navigationController popViewControllerRetro];
}

#pragma mark Collection view layout things

// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat fixSize = ([UIScreen mainScreen].bounds.size.width / 3) - 2;
    return CGSizeMake(fixSize, fixSize);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

Related posts

Recent Viewed