The main issue is that the cell doesn’t (and shouldn’t) have any idea about 
what size the collectionView is.  You could actually use the delegate though 
(which has a wider view) and provide the size that way.

That said, I did additional research and found other people with your issue.  
Relevant posts:
        https://github.com/imyoungyang/DynamicHeight
        
http://stackoverflow.com/questions/25895311/uicollectionview-self-sizing-cells-with-auto-layout

My recommendation would be to remove the preferredAttributes stuff, and give 
your cell’s content view an explicit width constraint. Then in the 
collectionView:layout:sizeForItemAtIndexPath: method of your 
FlowLayoutDelegate, set the constant of the width constraint to the desired 
width (you are passed the collectionView). Then return the result of 
contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).  Note: 
I haven’t actually tried this… all code written in mail.

Thanks,
Jon

> On Aug 10, 2016, at 2:27 PM, Doug Hill <cocoa...@breaqz.com> wrote:
> 
> Jonathon,
> 
> Thanks for the feedback.
> 
> A question that comes to mind is, what about making cells the same size as 
> the collection view requires going through subclassing the collection view 
> layout? Apple documentation IMPLIES this should work. It even documents that 
> developers should use preferredLayoutAttributesFittingAttributes for this 
> very purpose.
> 
> The reason I don't want to subclass flow layout is that I pretty much want 
> the exact functionality the default flow layout provides:
> 
> 1. Automatically calculating layout rects that flow across lines.
> 2. Calculating the height of cells dynamically at runtime via 
> 'estimatedItemSize'
> 
> Given that, I'm open to ideas on what I should override in a layout subclass. 
> Particularly ones that don't require me to reimplement #1 and #2 above.
> 
> Doug Hill
> 
>> On Aug 10, 2016, at 2:16 PM, Jonathan Hull <jh...@gbis.com 
>> <mailto:jh...@gbis.com>> wrote:
>> 
>> Because you are trying to make the width of the cell the same size as the 
>> collection view, I would strongly consider writing a small subclass of flow 
>> layout.  It honestly sounds like less work than what you are dealing with 
>> now.
>> 
>> Thanks,
>> Jon
>>  
>>> On Aug 10, 2016, at 1:29 PM, Doug Hill <cocoa...@breaqz.com 
>>> <mailto:cocoa...@breaqz.com>> wrote:
>>> 
>>>> 
>>>> On Aug 10, 2016, at 11:10 AM, Doug Hill <cocoa...@breaqz.com 
>>>> <mailto:cocoa...@breaqz.com>> wrote:
>>>> 
>>>> 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.
>>>> 
>>> 
>>> Again, looking for any ideas, pointers, etc. about any of this, including 
>>> whether I'm going about this the wrong way.
>>> 
>>> 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