Keeping the contentOffset in a UICollectionView while rotating Interface Orientation

后端 未结 24 1369
野性不改
野性不改 2020-12-04 07:22

I\'m trying to handle interface orientation changes in a UICollectionViewController. What I\'m trying to achieve is, that I want to have the same contentOffset afte

24条回答
  •  旧巷少年郎
    2020-12-04 08:21

    I have a similar case in which i use this

    - (void)setFrame:(CGRect)frame
    {
        CGFloat currentWidth = [self frame].size.width;
        CGFloat offsetModifier = [[self collectionView] contentOffset].x / currentWidth;
    
        [super setFrame:frame];
    
        CGFloat newWidth = [self frame].size.width;
    
        [[self collectionView] setContentOffset:CGPointMake(offsetModifier * newWidth, 0.0f) animated:NO];
    }
    

    This is a view that contains a collectionView. In the superview I also do this

    - (void)setFrame:(CGRect)frame
    {    
        UICollectionViewFlowLayout *collectionViewFlowLayout = (UICollectionViewFlowLayout *)[_collectionView collectionViewLayout];
    
        [collectionViewFlowLayout setItemSize:frame.size];
    
        [super setFrame:frame];
    }
    

    This is to adjust the cell sizes to be full screen (full view to be exact ;) ). If you do not do this here a lot of error messages may appear about that the cell size is bigger than the collectionview and that the behaviour for this is not defined and bla bla bla.....

    These to methods can off course be merged into one subclass of the collectionview or in the view containing the collectionview but for my current project was this the logical way to go.

提交回复
热议问题