Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error updating width #16

Open
eli7ah opened this issue Sep 20, 2018 · 8 comments
Open

Error updating width #16

eli7ah opened this issue Sep 20, 2018 · 8 comments

Comments

@eli7ah
Copy link

eli7ah commented Sep 20, 2018

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
Copy link
Author

eli7ah commented Sep 26, 2018

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

@StackHelp
Copy link

@eli7ah same issue here

@ivarunpatel
Copy link

ivarunpatel commented Mar 11, 2019

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

   override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.collectionView.collectionViewLayout.invalidateLayout()

    }

@mischa-hildebrand
Copy link
Owner

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.

@ivarunpatel
Copy link

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

@mischa-hildebrand
Copy link
Owner

mischa-hildebrand commented Mar 12, 2019

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
Copy link
Owner

💡 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.

@xiangBiaoOne
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants