I'm currently trying to implement something that seems basic but has been 
driving me nuts: making a Collection View with cells that are dynamic-width and 
height at runtime.

I was able to accomplish this with Table Views by using the dynamic height 
constant. Following Apple documentation, I set the width of the table view cell 
in IB to be an arbitrary size, for example using the default 600 pixels. I then 
use autolayout to make the size/position of the table view subviews adjust to 
the size of the cell. At runtime, the table view sets the width of the cell, 
and autolayout can change the height based on the content (e.g. multi-line 
labels.)

Now I want to do this using a Collection View. Reading Apple's documentation 
and watching the relevant WWDC video, it looks like this will be just as easy, 
right? As is my experience working with Collections, nothing ever comes easy.

OK, so I do basically the same thing as I was doing with tables.

• Create a Collection View Controller in a Storyboard.
• Create a collection view cell in the collection view inside the Storyboard.
• Make the cell the width of the collection view.
• Add some labels that fit the width of the cell using autolayout and allow for 
multiple lines (e.g. dynamic height)
• Use the default flow layout in IB and set the item size in the flow layout to 
be something less than the size of the collection view.
• Make sure section insets are 0.
• At runtime during viewDidLoad for the collection view controller, set the 
item size and estimated item size in the flow layout to be something related to 
the size of collection

I run this and behold…it crashes. And there are lots of warnings:

"The behavior of the UICollectionViewFlowLayout is not defined because: the 
item width must be less than the width of the UICollectionView minus the 
section insets left and right values, minus the content insets left and right 
values."

This is kind of odd since I go through a lot of trouble to make sure the cell 
item size is not too big. Here is what I see for the item size: Yet the app 
still crashes due to a cell that is too wide for the collection.

So, somewhere else the item size is getting set behind my back to something 
other than what I set in itemSize, estimatedItemSize, cell bounds, etc.

Before I go any further, I wanted to see if there's an official way to 
accomplish my goals without doing way more work, that is having to subclass the 
flow layout class. Should the default flow layout class even be able to do 
this? I see a lot of different ideas on the interwebs that involve doing things 
with the flow layout but I'm hesitant to go down that path. Shouldn't this very 
basic use case be doable without having to get into the complexity of the 
layout class? Honestly, the documentation for this is terrible. There's a lot 
of hand-waving and assumptions that don't quite spell out most of the details 
of this use case.

Thanks.

Doug Hill
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to