And important fuctions.

“Have to override this 3 fuctions” for layout working right.
‘layoutAttributesForItem’ and ‘layoutAttributesForSupplementaryView’ (if needed) and ‘layoutAttributesForElements’

class FlowLayout: UICollectionViewFlowLayout {
    
    private var cachedAttributes: [UICollectionViewLayoutAttributes] = []
    private var contentSize: CGSize = .zero
    
    override var collectionViewContentSize: CGSize {
        contentSize
    }
    
    override func prepare() {
        super.prepare()
        guard let cv = collectionView else { return }
        
        let numberOfSections = cv.numberOfSections
        (0..<numberOfSections).forEach { section in
            let numberOfItems = cv.numberOfItems(inSection: section)
            
            (0..<numberOfItems).forEach { item in
                let attr = UICollectionViewLayoutAttributes(forCellWith: .init(item: item, section: section))
                cachedAttributes.append(attr)
            }
        }
        
        contentSize = .init(width: cv.bounds.width, height: 100)
    }
    
    override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        cachedAttributes.first { $0.representedElementCategory == .supplementaryView && $0.indexPath == indexPath }
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        cachedAttributes.first { $0.representedElementCategory == .cell && $0.indexPath == indexPath }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        cachedAttributes
    }
   
}