Why does UICollectionView log an error when the cells are fullscreen?

后端 未结 10 1327
悲&欢浪女
悲&欢浪女 2020-12-08 04:06

I have a UICollectionViewController using a UICollectionViewFlowLayout where my itemSize is the size of the UICollectionView

10条回答
  •  温柔的废话
    2020-12-08 04:47

    Like Stunner, I had the same problem when rotating from landscape (picture using full width) to portrait mode. His suggestion was the only one which really helped.

    Attached the code with latest Swift for his ObjC example ... and as a bonus, my code to find the center cell of the collection view. Works quite nice ;-)

    /**
     -----------------------------------------------------------------------------------------------
    
     viewWillTransition()
    
     -----------------------------------------------------------------------------------------------
     */
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    
        // find cell in the center of the screen and store it in a global variable
        let center = self.view.convert((self.collectionView!.center), to: self.collectionView)
    
        // get the indexPath for the cell in the center of the current screen
        if let index = collectionView!.indexPathForItem(at: center) {
    
            // store it in a class property
            self.indexPathOfCenterCell = index
        }
    
        // force recalculation of margins, borders, cell sizes etc.
        self.collectionView?.collectionViewLayout.invalidateLayout()
    
        // inform UIKit about it    
        super.viewWillTransition(to: size, with: coordinator)
    }
    

提交回复
热议问题