gngrwzrd
gngrwzrd

Reputation: 6022

UICollectionView always auto sizing cells. Not using sizes returned from delegate

Recently upgraded to Xcode 11 / iOS13. Working on a new view controller. The UICollectionView is calling my sizeForItemAtPath delegate method, but it seems like the collection view is always choosing self sizing based on constraints. But I don't want it to!

I tried setting the estimatedSize to 1x1 just to try and work around this. Didn't work. I've breakpointed on all the delegate / datasource methods and don't see anything out of the ordinary.

I also came across this (`systemLayoutSizeFittingSize` not called on iOS 13), maybe my issue is related? I am able to fix the issue by overriding the systemFittingSize method in that link - but I really shouldn't have to.

The odd thing I find is that any existing xibs with collection views are working as expected - the only difference seems to be me creating the cell / collection view with Xcode 11 VS Xcode 10.

I also noticed that UICollectionViewCell elements in Xcode11 contain a "Content View" - but Xcode 10 xibs don't have a Content View.

Anyone else experience this?

Upvotes: 7

Views: 536

Answers (2)

WTEDST
WTEDST

Reputation: 955

I actually have never worked with XIBs or flow layouts, but I am currently working on some custom collection views: from what I know, in the most general case, it's not the collection view itself who's "choosing self-sizing", it's the layout object.

Collection view on itself or it's delegate do not have any methods that ask cells for their preferred sizes. On the other hand, the flow layout does. It is known that flow layout checks internally if a cell uses auto layout, and, if the cell does, it ignores the information supplied by the delegate.

One can opt out of that behavior by subclassing the layout and returning false in shouldInvalidateLayout(forPreferredLayoutAttributes:, withOriginalAttributes:).

Upvotes: 0

gngrwzrd
gngrwzrd

Reputation: 6022

There's a setting in interface builder for estimated size - set it to None.

enter image description here

Change to None:

enter image description here

Upvotes: 8

Related Questions