AlignedCollectionViewFlowLayout icon indicating copy to clipboard operation
AlignedCollectionViewFlowLayout copied to clipboard

Error updating width

Open eli7ah opened this issue 7 years ago • 8 comments

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!

eli7ah avatar Sep 20 '18 10:09 eli7ah

If anybody meet such a problem too please tell the alternative library please

eli7ah avatar Sep 26 '18 13:09 eli7ah

@eli7ah same issue here

StackHelp avatar Sep 27 '18 03:09 StackHelp

I was facing same issue. To fix you need invalidateLayout like below:

   override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.collectionView.collectionViewLayout.invalidateLayout()

    }

ivarunpatel avatar Mar 11 '19 10:03 ivarunpatel

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 avatar Mar 11 '19 10:03 mischa-hildebrand

@mischa-hildebrand Ohh..!!! What is an alternative solution to this problem?

ivarunpatel avatar Mar 11 '19 12:03 ivarunpatel

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:

  1. Either it's simply a system bug introduced by Apple in recent iOS releases.
  2. 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.

mischa-hildebrand avatar Mar 12 '19 05:03 mischa-hildebrand

💡 Hint:

You can try the following to fix this issue:

  1. As the last step in your viewDidLoad() method (and subsequently whenever you changed the size of your items), invalidate the flow layout by calling flowLayout?.invalidateLayout().

  2. In your data source method cellForItem(at:), call cell.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.

mischa-hildebrand avatar Mar 12 '19 06:03 mischa-hildebrand

if collectionView superView is View Rather than ViewController no viewDidLoad() method .What do I do?

xiangBiaoOne avatar May 08 '20 07:05 xiangBiaoOne