StuartM
StuartM

Reputation: 6823

Make UICollectionViewCell appear in center of UICollectionView

I have some cells which are dynamically added to the UICollectionView. What I would like to do is change the inset of these cells so they appear in the center of the UICollectionView. This is because the more cells that are added the cell height changes (it is a single Row UICollectionView).

- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // This method is used to center when one tile is entered else use a standard no inset
    if (self.wordArray.count == 1) {
        CGFloat newWidth = (self.tileCollectionView.bounds.size.width - self.tileCollectionView.bounds.size.height);
        return UIEdgeInsetsMake(0, (newWidth / 2), 0, (newWidth/2));
    } else {
        return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
    }
}

How do I get a reference to the UICollectionViewCell current height, for any of the cells as they are all shaped the same (just their height/width changes when more added to continue to fit to one row.

Once I have the reference to the cell height, I can add something like: self.tileCollectionView.bounds.size.height - CELLHEIGHT /2 -- to use as the inset for the cell.

Upvotes: 4

Views: 9129

Answers (2)

Velociround
Velociround

Reputation: 631

If all of your cells are the same size and assuming you are using UICollectionViewFlowLayout, you can use this to get their size:

UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
CGFloat width = flowLayout.itemSize.width;
CGFloat height = flowLayout.itemSize.height;

This is how I calculate my insets using UICollectionViewFlowLayout:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    NSInteger numberOfCells = self.view.frame.size.width / flowLayout.itemSize.width;
    NSInteger edgeInsets = (self.view.frame.size.width - (numberOfCells * flowLayout.itemSize.width)) / (numberOfCells + 1);
    return UIEdgeInsetsMake(0, edgeInsets, 0, edgeInsets);
}

You'll also need to make sure to invalidate the layout when the screen rotates to make it calculate the new insets again:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self.collectionView.collectionViewLayout invalidateLayout];
}

Upvotes: 2

Nick Terry
Nick Terry

Reputation: 171

If you're using a grid-style layout, I'd try this method: collectionView:layout:sizeForItemAtIndexPath:

You'll need to implement the UICollectionViewFlowLayoutDelegate to do so. To get them to stack, set the minimum offsets to negatives the size of the cells themselves. This may take some trial and error.

Upvotes: 1

Related Questions