Reputation: 1925
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?
Upvotes: 8
Views: 18760
Reputation: 1376
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;
}
Upvotes: 0
Reputation: 977
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);
}
Upvotes: 15