Change UICollectionViewCell size on different device orientations

后端 未结 6 596
刺人心
刺人心 2020-12-12 09:36

I am using an UICollectionView with UICollectionViewFlowLayout.

I set the size of each cell through the

collectionView:lay         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 10:25

    If you are using autolayout.You just need to invalidate layout in your view controller when rotating and adjust offset in your flow layout subclass.

    So, In your view controller -

    override func willRotateToInterfaceOrientation(toInterfaceOrientation:     UIInterfaceOrientation, duration: NSTimeInterval) {
        self.galleryCollectionView.collectionViewLayout.invalidateLayout()
    }
    

    And in your FlowLayout Subclass

    override func prepareLayout() {
        if self.collectionView!.indexPathsForVisibleItems().count > 0{
        var currentIndex : CGFloat!
        currentIndex = CGFloat((self.collectionView!.indexPathsForVisibleItems()[0] as! NSIndexPath).row)
        if let currentVal = currentIndex{
            self.collectionView!.contentOffset = CGPointMake(currentIndex * self.collectionView!.frame.width, self.collectionView!.contentOffset.y);
        }   
        }     
    }
    

提交回复
热议问题