Swift: How to refresh UICollectionView layout after rotation of the device

后端 未结 14 914
闹比i
闹比i 2020-12-01 02:38

I used UICollectionView (flowlayout) to build a simple layout. the width for each cell is set to the width of screen using self.view.frame.width

but whe

14条回答
  •  半阙折子戏
    2020-12-01 03:02

    When UICollectionLayout detects a bounds change, it asks if it needs to reroute the Invalidate layout. You can rewrite the method directly.UICollectionLayout can call invalidateLayout method at the right time

    class CollectionViewFlowLayout: UICollectionViewFlowLayout{
    
        /// The default implementation of this method returns false.
        /// Subclasses can override it and return an appropriate value
        /// based on whether changes in the bounds of the collection
        /// view require changes to the layout of cells and supplementary views.
        /// If the bounds of the collection view change and this method returns true,
        /// the collection view invalidates the layout by calling the invalidateLayout(with:) method.
        override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    
            return (self.collectionView?.bounds ?? newBounds) == newBounds
        }
    }
    

提交回复
热议问题