See Also: UICollectionViewLayout Members
Collection Views allow content to be displayed using arbitrary layouts. Grid-like layouts can use the UIKit.UICollectionViewFlowLayout or application developers can subtype UIKit.UICollectionViewLayout to create their own flexible pattern.
The layout of the UIKit.UICollectionViewCells in a UIKit.UICollectionView is controlled by a UIKit.UICollectionViewLayout, which can be passed in to the UICollectionView(UICollectionViewLayout) constructor or can changed with UICollectionView.SetCollectionViewLayout.
Application developers can create fully custom layouts by subclassing either UIKit.UICollectionViewFlowLayout or UIKit.UICollectionViewLayout.
The key methods to override are:
Method | Description |
---|---|
UICollectionViewLayout.PrepareLayout | Used for performing initial geometric calculations that will be used throughout the layout process. |
UICollectionViewLayout.CollectionViewContentSize | Returns the size of the area used to display content. |
UICollectionViewLayout.LayoutAttributesForElementsInRect | Returns the layout attributes for all the cells and views within the specified rectangle. |
UICollectionViewLayout.LayoutAttributesForItem | The layout attributes of a specific cell |
UICollectionViewLayout.ShouldInvalidateLayoutForBoundsChange | Returns true if the new bounds require an update of the layout. |
The following code, taken from the "Introduction to Collection Views" class, creates a circular layout, as shown in the following image:
C# Example
public override UICollectionViewLayoutAttributes LayoutAttributesForItem (NSIndexPath path) { UICollectionViewLayoutAttributes attributes = UICollectionViewLayoutAttributes.CreateForCell (path); attributes.Size = new SizeF (ItemSize, ItemSize); attributes.Center = new PointF (center.X + radius * (float)Math.Cos (2 * path.Row * Math.PI / cellCount), center.Y + radius * (float)Math.Sin (2 * path.Row * Math.PI / cellCount)); return attributes; }