Keeping the contentOffset in a UICollectionView while rotating Interface Orientation

后端 未结 24 1353
野性不改
野性不改 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:04

    This work like a charm:

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return self.view.bounds.size;
    }
    
    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    
        int currentPage = collectionMedia.contentOffset.x / collectionMedia.bounds.size.width;
        float width = collectionMedia.bounds.size.height;
    
        [UIView animateWithDuration:duration animations:^{
            [self.collectionMedia setContentOffset:CGPointMake(width * currentPage, 0.0) animated:NO];
            [[self.collectionMedia collectionViewLayout] invalidateLayout];
    }];
    }
    

提交回复
热议问题