Cell size not updating after changing flow layout's itemSize

It seems it's a bug of UICollectionViewFlowLayout, I have to call prepare() before invalidateLayout()

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()


    guard let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else
    {
        return
    }

    layout.itemSize = cellSize
    layout.minimumLineSpacing = space
    layout.prepare()  // <-- call prepare before invalidateLayout
    layout.invalidateLayout()
}

Change the collection view's 'Estimate Size' from the inspector to 'None'! None of the above answers worked for me.

Estimate Size