I'm now trying some techniques to fix the behavior to center items when there 
is one item in a collection view section.

I'm trying two different approaches:

1. Like Peter's suggestion, create a subclass of UICollectionViewFlowLayout, 
and override layout attributes methods

I create overrides of both layoutAttributesForItemAtIndexPath and 
layoutAttributesForElementsInRect. I then modify the 'frame' property of any 
layout attribute objects whose index path is Section == 0. I set the origin to 
0.

I see no effect at runtime.

I notice that the origin X value starts as 0 for these items before I attempt 
to modify it, so it doesn't look like making any changes in this method would 
have any effect. Somewhere else the item is being centered in the collection 
view behind my back.

2. Override preferredLayoutAttributesFittingAttributes in my custom collection 
cell object

Same as above, I notice that the origin X value of the frame property of layout 
attribs is already 0 before I do any modifications. And sure enough, 
modifications don't make any effect at runtime.

This behavior is baffling as I've seen other collection views that are 
implemented with the items left justified in multiple sections. Any ideas? Am I 
missing something?

Doug Hill


> On Aug 8, 2016, at 12:35 PM, Doug Hill <cocoa...@breaqz.com> wrote:
> 
> Hello Peter,
> 
> Thanks for the info. I'm still trying to see if I can do what I want with the 
> stock flow layout.
> 
> FWIW, it appears that the behavior of the flow layout class is to center 
> items when there is one section.
> 
> This appears to be a summary of the behavior:
> 
> Collection has one section:
> • Items on each line are centered on the line, including multiple items per 
> line.
> 
> Collection has multiple sections:
> • If one item in the section, center on the line.
> • Multiple items per section, flow from left-to-right, including across lines.
> 
> I wish the multiple section case was consistent flowing left-to-right.
> 
> Doug
> 
> 
>> On Aug 3, 2016, at 1:12 PM, Peter Tomaselli <vast.gra...@gmail.com> wrote:
>> 
>> I remain a non-expert on this topic, but my assumption has always been that 
>> the current “look” is by design. Ugly, but by design. “Equally distribute 
>> cells across the row” could be taken to mean that in the case of one cell, 
>> it belongs in the middle… [shrug]
>> 
>> This is some of the first Cocoa code I wrote so please forgive… almost 
>> everything about it, but here’s the FlowLayout subclass I mentioned: 
>> https://github.com/Peterbing/CV-AutoLayout/blob/master/TemplateProject/TheFlowLayout.m#L31
>>  
>> <https://github.com/Peterbing/CV-AutoLayout/blob/master/TemplateProject/TheFlowLayout.m#L31>
>> 
>> This is a completely “left-justified” layout and it works by overriding the 
>> `layoutAttributesFor*` methods to call super, and then squishes everything 
>> over to the left before returning the attributes to the caller. 
>> 
>> This is certainly not production-quality code but perhaps it’s a start! IIRC 
>> there is Apple documentation (or perhaps it was a WWDC session) about 
>> creating a simple “tweaked” UICollectionViewFlowLayout subclass — so my 
>> impression is that this approach in general is kosher.
>> 
>> On Wed, Aug 3, 2016 at 3:38 PM, Doug Hill <cocoa...@breaqz.com 
>> <mailto:cocoa...@breaqz.com>> wrote:
>> So, does anyone know if the current behavior I mentioned is a bug? Is the 
>> behavior of Flow Layout documented? Should I file a bug with Apple?
>> 
>> Also, what would be some quick ways to modify the flow layout behavior to 
>> handle this case with one item per section?
>> 
>> 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/cocoadev%40breaqz.com
> 
> This email sent to cocoa...@breaqz.com


_______________________________________________

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