How do I define the size of a CollectionView on rotate

后端 未结 7 785
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 15:26

I have a viewcontroller with 2 CollectionView Controllers.

I would like on rotation that only one of the collection views resize to a custom size while the other re

7条回答
  •  执念已碎
    2020-12-04 16:01

    I've solved this problem by checking the rotation of the device and then reloading the layout. Checking the screen size and using this size to display my cell.

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        //get new width of the screen
        screenRect = [[UIScreen mainScreen] bounds];
        screenWidth = screenRect.size.width;
    
        //if landscape mode
        if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
        {
            screenDivide = 5;
        }
        else
        {
            screenDivide = 3;
        }
    
        //reload the collectionview layout after rotating
        UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
        [layout invalidateLayout];
    }
    

    Then I defined the cells like this

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        screenRect = [[UIScreen mainScreen] bounds];
        screenWidth = screenRect.size.width;
    
        CGSize elementSize = CGSizeMake(screenWidth/screenDivide, 100);
        return elementSize;
    }
    

提交回复
热议问题