Iglistkit: Device rotation causes invalid size issue

Created on 22 Jun 2018  路  2Comments  路  Source: Instagram/IGListKit

This question is similar to #1095, however the answer doesn't fix my problem. When I rotate my device from portrait to landscape, the width of the cell inside my collection view stays the same width as it was in portrait, although the height changes correctly.

For example, suppose I have some text that looks like this when my device is in portrait mode:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam 
accumsan neque sit amet velit efficitur, venenatis

When I turn the phone into landscape mode the whole text should fit on one line but instead it looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam

It cuts off the rest of the text that should be on that line. To fix this I tried calling performUpdates within viewWillTransition(from:to:) in my view controller like this:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    adapter.performUpdates(animated: true)
}

.. but it doesn't work and gives the error:

UICollectionViewFlowLayout instance is <MyApp.UICollectionViewFlowLayout: 0x10bdcf0e0>, and it is
attached to <UICollectionView: 0x106906600; frame = (0 401; 265 200); transform = [1, 0, 0, -1, 0, 0];
clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x10bdce880>; 
animations = { bounds.origin=<CABasicAnimation: 0x10c9647c0>; bounds.size=<CABasicAnimation: 
0x10c9648b0>; position=<CABasicAnimation: 0x10c5b82b0>; }; layer = <CALayer: 0x10d7205e0>; 
contentOffset: {0, 0}; contentSize: {557, 49}; adjustedContentInset: {0, 0, 0, 0}> collection view
layout: <MyApp.UICollectionViewFlowLayout: 0x10bdcf0e0>.
2018-06-22 15:02:31.625103+0100 QJAM[15682:3401723] Make a symbolic breakpoint at 
UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

Any subsequent cells that get added to the collection view when in landscape mode do size correctly though. How can I fix this?

Most helpful comment

I think you need to invalidate the layout in viewWillTransition instead of performing items update
E.g.

invalidateLayout(with: invalidationContext(forBoundsChange: .zero))

All 2 comments

I think you need to invalidate the layout in viewWillTransition instead of performing items update
E.g.

invalidateLayout(with: invalidationContext(forBoundsChange: .zero))

@weyert is right. This isn鈥檛 anything to do with IGListKit either, just a collection view thing.

Sent with GitHawk

Was this page helpful?
0 / 5 - 0 ratings