user2312407
user2312407

Reputation: 67

UICollectionView alignment of cells

I have a UICollectionView with 2 different types of UICollectionViewCells. There is a tab that prompts the showing of each type of cell and only one type of cell can be visible at a time.

For some reason, 1 type of cell aligns left in the UICollectionView, but the other cell horizontally aligns in the center. I'd like both types of cells to align left.

Edge insets are:

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

Any help would be much appreciated.

Upvotes: 2

Views: 5304

Answers (1)

rdelmar
rdelmar

Reputation: 104092

The cells will layout differently depending on their size, so you need to use different insets for each of your cell types. I think it should be doable like this:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section];
    if ([cell isKindOfClass:[MyClass1 class]]){
        return UIEdgeInsetsMake(10,10,10,10);
    }else{
        return UIEdgeInsetsMake(20,10,10,10);
}

Substitute your correct class name for MyClass1 and experiment with the edge insets until you get the behavior you want.

Upvotes: 2

Related Questions