How do I define the size of a CollectionView on rotate

后端 未结 7 787
被撕碎了的回忆
被撕碎了的回忆 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:12

    I've handled this by detecting the orientation change in the setBounds: method. We subclass the UICollectionView and override the setBounds: method (see below). In flowLayoutForGallerySize we create a new UICollectionViewFlowLayout with different spacing and itemSize depending on the size of the collection view.

    - (void)setBounds:(CGRect)bounds
    {
        // detect orientation changes and update itemSize in the flow layout
        CGSize oldSize = self.bounds.size;
    
        [super setBounds:bounds];
    
        BOOL wasLandscape = (oldSize.width > oldSize.height);
        BOOL nowLandscape = (bounds.size.width > bounds.size.height);
    
        if (wasLandscape != nowLandscape) {
            // create a new flow layout object for the new gallery size
            self.flowLayout = [self flowLayoutForGallerySize:bounds.size];
    
            // change to the new flow layout
            [self setCollectionViewLayout:self.flowLayout animated:NO];
    
            DLog(@"orientation change to %@", NSStringFromCGSize(bounds.size));
        }
    }
    

提交回复
热议问题