Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView layout update - item size not changing

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?

like image 420
Armands L. Avatar asked May 19 '14 13:05

Armands L.


1 Answers

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);
}
like image 146
Nazariy Vlizlo Avatar answered Oct 14 '22 12:10

Nazariy Vlizlo