Collection view

collection view 1- Using the Flo

2017-03-25  本文已影响18人  轻云绿原

内容来源:https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1

Using the Flow layout

怎样使用 UICollectionViewFlowLayout

1.创建flow layout 对象,并赋值给collection view.

在创建collection view时,有个初始化方法的形参就是layout:

UICollectionView(frame:CGRect, collectionViewLayout:UICollectionViewLayout)

2.配置cells的高度和宽度。

3.设置行(lines)和项(items)的最小间隔。(可选的)

4.如果你要section headers和footers,具体说明它们的尺寸。

5.设置滚动方向。默认是垂直方向。

重要说明:你必须说明cells的高宽。否则,你的items高宽就被赋值为零,那将不可见。


items 之间的实际空间大于等于最小值。


Knowing When to Subclass the Flow Layout

Scenario

Subclassing tips

1.You want to add new supplementary or decoration views to your layout

The standard flow layout class supports only section header and section footer views and no decoration views. To support additional supplementary and decoration views, you need to override the following methods at a minimum:

layoutAttributesForElementsInRect:(required)

layoutAttributesForItemAtIndexPath:(required)

layoutAttributesForSupplementaryViewOfKind:atIndexPath:(to support new supplementary views)

layoutAttributesForDecorationViewOfKind:atIndexPath:(to support new decoration views)

In yourlayoutAttributesForElementsInRect:method, you can callsuperto get the layout attributes for the cells and then add the attributes for any new supplementary or decoration views that are in the specified rectangle. Use the other methods to provide attributes on demand.

For information about providing attributes for views during layout, seeCreating Layout AttributesandProviding Layout Attributes for Items in a Given Rectangle.

2.You want to tweak the layout attributes being returned by the flow layout

Override thelayoutAttributesForElementsInRect:method and any of the methods that return layout attributes. The implementation of your methods should callsuper, modify the attributes provided by the parent class, and then return them.

For in-depth dicussions of what these methods entail, seeCreating Layout AttributesandProviding Layout Attributes for Items in a Given Rectangle.

3.You want to add new layout attributes for your cells and views

Create a custom subclass ofUICollectionViewLayoutAttributesand add whatever properties you need to represent your custom layout information.

SubclassUICollectionViewFlowLayoutand override thelayoutAttributesClassmethod. In your implementation of that method, return your custom subclass.

You should also override thelayoutAttributesForElementsInRect:method, thelayoutAttributesForItemAtIndexPath:method, and any other methods that return layout attributes. In your custom implementations, you should set the values for any custom attributes you defined.

4.You want to specify initial or final locations for items being inserted or deleted

By default, a simple fade animation is created for items being inserted or deleted. To create custom animations, you must override some or all of the following methods:

initialLayoutAttributesForAppearingItemAtIndexPath:

initialLayoutAttributesForAppearingSupplementaryElementOfKind:atIndexPath:

initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath:

finalLayoutAttributesForDisappearingItemAtIndexPath:

finalLayoutAttributesForDisappearingSupplementaryElementOfKind:atIndexPath:

finalLayoutAttributesForDisappearingDecorationElementOfKind:atIndexPath:

In your implementations of these methods, specify the attributes you want each view to have prior to being inserted or after they are removed. The flow layout object uses the attributes you provide to animate the insertions and deletions.

If you override these methods, it is also recommended that you override theprepareForCollectionViewUpdates:andfinalizeCollectionViewUpdatesmethods. You can use these methods to track which items are being inserted or deleted during the current cycle.

For more information about how insertions and deletions work, seeMaking Insertion and Deletion Animations More Interesting.

上一篇下一篇

猜你喜欢

热点阅读