Error updating width
I'm using alignedFlowLayout this way:
alignedFlowLayout.horizontalAlignment = .left
alignedFlowLayout.verticalAlignment = .top
After XCode updates to 10.0 (but still using Swift 4.0) flow layout requires to update manually after collection view reloaded:
alignedFlowLayout.invalidateLayout()
If there is an any other way for correct using or please fix an issue Thanx a lot!
If anybody meet such a problem too please tell the alternative library please
@eli7ah same issue here
I was facing same issue. To fix you need invalidateLayout like below:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.collectionView.collectionViewLayout.invalidateLayout()
}
I wouldn't recommend invalidating the layout in viewDidLayoutSubviews(). That method is called when the view has just finished its layout. Invalidating the collection view's layout at that point might trigger a recursion loop.
@mischa-hildebrand Ohh..!!! What is an alternative solution to this problem?
I honestly don't know. But I'm always open to suggestions!
The problem is the following:
The workflow of AlignedCollectionViewFlowLayout is that it asks its superclass (UICollectionViewFlowLayout) for layout attributes (those include the frames / sizes of the cells) and it assumes those layout attributes to be correct. However, Apple has made some changes to the internal layout process which we don't have any control over. For that reason, the superclass methods deliver the wrong layout attributes. (They just have their size set to their estimatedItemSize, not their actual dynamic size.)
You can confirm this by replacing AlignedCollectionViewFlowLayout with UICollectionViewFlowLayout in your project. The same problem should occur.
So for me it seems like it's either of the following two things:
- Either it's simply a system bug introduced by Apple in recent iOS releases.
- Or we're using it incorrectly and Apple expects us to apply a different approach to set it up.
Either way, we can't really do anything about it in this framework. However, please share any hints how to tackle this issue in this thread.
💡 Hint:
You can try the following to fix this issue:
-
As the last step in your
viewDidLoad()method (and subsequently whenever you changed the size of your items), invalidate the flow layout by callingflowLayout?.invalidateLayout(). -
In your data source method
cellForItem(at:), callcell.layoutIfNeeded()as the last step before returning your configured cell.
While I really don't fancy this solution (it mixes layout with data) and think that it should work out of the box once you plug it in, this method will most likely yield the desired results, without messing with the layout system.
if collectionView superView is View Rather than ViewController no viewDidLoad() method .What do I do?