• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Dynamic-width Collection View
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dynamic-width Collection View


  • Subject: Re: Dynamic-width Collection View
  • From: Jonathan Hull <email@hidden>
  • Date: Wed, 10 Aug 2016 15:18:41 -0700

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 <email@hidden> 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 <email@hidden <mailto:email@hidden>> 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 <email@hidden <mailto:email@hidden>> wrote:
>>>
>>>>
>>>> On Aug 10, 2016, at 11:10 AM, Doug Hill <email@hidden <mailto:email@hidden>> 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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Dynamic-width Collection View
      • From: Doug Hill <email@hidden>
    • Re: Dynamic-width Collection View
      • From: Peter Tomaselli <email@hidden>
References: 
 >Dynamic-width Collection View (From: Doug Hill <email@hidden>)
 >Re: Dynamic-width Collection View (From: Doug Hill <email@hidden>)
 >Re: Dynamic-width Collection View (From: Doug Hill <email@hidden>)

  • Prev by Date: Re: Dynamic-width Collection View
  • Next by Date: Re: Dynamic-width Collection View
  • Previous by thread: Re: Dynamic-width Collection View
  • Next by thread: Re: Dynamic-width Collection View
  • Index(es):
    • Date
    • Thread