UICollectionView Layout Update - Element Size Does Not Change

I have a problem with the size of the collectionView element. I want to display 3 elements per line for portrait mode and 6 for landscape. I configured -layoutSubviews as follows:

- (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 the cell size is not updated, it always remains unchanged for both orientations. If I create 2 flowLayouts and use:

 [self.collectionView setCollectionViewLayout:self.flowLayoutLandscape]; 

Everything works, but I really don't like how they change. The animation is really bad. And since itemSize is the only property that needs to be updated, using 1 layout seems like the best option.

How do I tell CollectionView to update the layout?

+6
source share
2 answers

I used this method, and it was very good for me:

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (_isLandscape) return CGSizeMake(yourLandscapeWidth, yourLandscapeHeight); else return CGSizeMake(yourNonLandscapeWidth, yourNonLandscapeHeight); } 
+12
source

Maybe you can use another cell for portrait / landscape mode with different identifiers. than you just need to reload the data to view the collection, than it will 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; } 
0
source

Source: https://habr.com/ru/post/969528/


All Articles