openradar-mirror icon indicating copy to clipboard operation
openradar-mirror copied to clipboard

18078598: UICollectionView calculates contentSize wrong with self-sizing cells

Open openradar-mirror opened this issue 8 years ago • 11 comments

Description

Summary: Using iOS8's self-sizing cells, UICollectionView calculates the size of the cells based on their auto layout constraints. But, as this example shows, it fails to use those numbers to calculate the overall collectionViewContentSize correctly.

This has the effect that some of the collection view's items are laid out beyond the scrollable region of the collection view.

Steps to Reproduce:

  1. Run the enclosed app, which reproduces the redoubtable Olivier Gutknecht's demo in WWDC 2014, session 226.
  2. Scroll to the bottom of the collection view.

Expected Results:

  1. I expected to the see that last collection view item laid out with the others.

Actual Results: 3. Notice that you cannot see the multiline label, which is the last item in the collection view

Version: iOS8beta6

Notes: This included example project includes discussion in the comments in ViewController.swift, and a short video showing the problem in Simulator.app

Configuration: iPhone 5

Attachments: 'SelfSizingCellsDemo.zip' was successfully uploaded.

Attachment available at: https://github.com/algal/SelfSizingCellsDemo/tree/attached-to-radar18078598

Product Version: iOS8beta6 Created: 2014-08-20T19:37:22.663837 Originated: 2014-08-20T00:00:00 Open Radar Link: http://www.openradar.me/18078598

openradar-mirror avatar Aug 11 '15 09:08 openradar-mirror

iOS 11 Issue still exists =[

ybeapps avatar Feb 19 '18 21:02 ybeapps

iOS 12 still on 🤞 These features are for WWDC demos only

richardtop avatar Jul 02 '18 12:07 richardtop

I ain’t using self sizing cells anymore When I tried using it I waisted weeks of my life for nothing Without it I finished my work in about 2 days! ;)

ybeapps avatar Jul 02 '18 13:07 ybeapps

@ybeapps what did you used instead of the self-sizing cells? I have a similar experience, I'm surprised, how the hackathon-level code goes into system frameworks at @apple.

richardtop avatar Jul 02 '18 13:07 richardtop

I wanted the cells to be bigger when selected I gave up on this but as far as I remember I used invalidationContext to add a margin in both sides of the selected cell (in my case the selected cell is always centered)

‫ב-2 ביולי 2018, בשעה 16:41, ‏‏Richard Topchii ‏[email protected] כתב/ה:‬

@ybeapps what did you used instead of the self-sizing cells?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ybeapps avatar Jul 02 '18 14:07 ybeapps

Thanks for reply, but how did you calculate the size and implemented the layout inside the cell? Invalidation context is related to the cell update/reload, where I was mostly interested in the layout technique you've used. Thanks.

richardtop avatar Jul 02 '18 14:07 richardtop

I need to check what I’ve done I can’t remember right now I only can say that I didn’t used self-sizing cells mechanism

‫ב-2 ביולי 2018, בשעה 17:31, ‏‏Richard Topchii ‏[email protected] כתב/ה:‬

Thanks for reply, but how did you calculate the size and implemented the layout inside the cell? Invalidation context is related to the cell update/reload, where I was mostly interested in the layout technique you've used. Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ybeapps avatar Jul 02 '18 15:07 ybeapps

The last line is clear without any doubt 🤣 I would be really interested in what approach you've used for the layout inside the cells.

richardtop avatar Jul 02 '18 16:07 richardtop

My cells are very simple It’s just a UILabel with a number I’m not resizing them, just reposition

When selection changes I call this:

func invalidatePositions() { let ctx = UICollectionViewFlowLayoutInvalidationContext() ctx.invalidateFlowLayoutAttributes = true ctx.invalidateFlowLayoutDelegateMetrics = false invalidateLayout(with: ctx) }

I’m overriding UICollectionViewFlowLayout to achieve what I want with positioning.

And I’m using the power of UICollectionViewDelegate

The delegate and the flowLayout subclasses gives me almost full control on what happening

The cell is very simple, it has nothing special.

‫ב-2 ביולי 2018, בשעה 19:31, ‏‏Richard Topchii ‏[email protected] כתב/ה:‬

The last line is clear without any doubt 🤣 I would be really interested in what approach you've used for the layout inside the cells.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ybeapps avatar Jul 02 '18 17:07 ybeapps

Got it, thanks! My cells are a bit more complex and I don't need to have such level of interactivity. My CollectionView is heterogeneous, similar to AppStore.

richardtop avatar Jul 02 '18 19:07 richardtop

Any news on this? Having the same issue.

First load works fine, reloading data with the same data source (after first reloading with a different data source) is sending a bigger or smaller content size, depending on the size of the previous data source, but did not find any rule for it.

My solution was to use the fact that first load always sends correct content size, and I removed and re-added the collection view altogether from and to it's superview, never reload the data, but this is a stupid hack, although it works.

I am curious if anybody finds a correct solution.

bogdanbalta avatar Aug 08 '19 14:08 bogdanbalta