Using the Flow layout
怎樣使用 UICollectionViewFlowLayout
1.創(chuàng)建flow layout 對(duì)象坛梁,并賦值給collection view.
在創(chuàng)建collection view時(shí)客年,有個(gè)初始化方法的形參就是layout:
UICollectionView(frame:CGRect, collectionViewLayout:UICollectionViewLayout)
2.配置cells的高度和寬度往枷。
3.設(shè)置行(lines)和項(xiàng)(items)的最小間隔。(可選的)
4.如果你要section headers和footers,具體說明它們的尺寸锅铅。
5.設(shè)置滾動(dòng)方向。默認(rèn)是垂直方向简烤。
重要說明:你必須說明cells的高寬果覆。否則呜笑,你的items高寬就被賦值為零,那將不可見彻犁。
items 之間的實(shí)際空間大于等于最小值叫胁。
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.