UICollectionView layout update - item size not changing
Asked Answered
M

2

8

I'm having a problem with collectionView's item size. I want to display 3 items in row for Portrait mode and 6 for landscape. I've set up -layoutSubviews like this:

- (void)layoutSubviews {
    [super layoutSubviews];

    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)) {
        //Portrait orientation
        self.flowLayoutPortrait.itemSize = CGSizeMake(106, 106);

    } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) {
        //Landscape orientation
        self.flowLayoutPortrait.itemSize = CGSizeMake(93, 93);
    }
    [self.collectionView.collectionViewLayout invalidateLayout];
}

But cell size does not get updated, it always stay the same for both orientations. If I create 2 flowLayouts and the use:

[self.collectionView setCollectionViewLayout:self.flowLayoutLandscape];

Everything works, but I really don't like how they are changed. Animation is really bad. And because itemSize is the only property that needs to be updated, using 1 layout seems like best option.

How can I tell collectionView to update layout?

Molli answered 19/5, 2014 at 13:2 Comment(0)
C
15

I used this method and this was for me very fine:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isLandscape)
        return CGSizeMake(yourLandscapeWidth, yourLandscapeHeight);
    else
        return CGSizeMake(yourNonLandscapeWidth, yourNonLandscapeHeight);
}
Command answered 19/5, 2014 at 14:2 Comment(1)
Thanks, this works great! For future readers, it's also necessary to add [collectionView.collectionViewLayout invalidateLayout] in willRotateToInterfaceOrientation:Molli
D
0

May be you can different cell for portrait/landscape mode with different identifiers .. than you just have to reload data for collection view than it 'll definitely work.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)) {
        //Portrait orientation
        //dequeue cell for portrait mode

    } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) {
        //Landscape orientation
        //dequeue cell for landscape mode
    }

    return cell;
}
Dimorphism answered 19/5, 2014 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.