UICollectionViewLayoutAttributes Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 6.0 and later.
Declared in
UICollectionViewLayout.h

Overview

An UICollectionViewLayoutAttributes object manages the layout-related attributes for a given item in a collection view. Layout objects create instances of this class when asked to do so by the collection view. In turn, the collection view uses the layout information to position cells and supplementary views inside its bounds.

Subclassing Notes

In most cases, you use this class as-is. If you want to supplement the base layout attributes with custom layout attributes, you can subclass and define whatever properties you want to store the additional layout data. Because layout attribute objects may be copied by the collection view, make sure your subclass conforms to the NSCopying protocol by implementing any methods appropriate for copying your custom attributes to new instances of your subclass. In addition to defining your subclass, your UICollectionReusableView objects need to implement the applyLayoutAttributes: method so that they can apply any custom attributes at layout time.

If you subclass and implement any custom layout attributes, you must also override the inherited isEqual: method to compare the values of your properties. In iOS 7 and later, the collection view does not apply layout attributes if those attributes have not changed. It determines whether the attributes have changed by comparing the old and new attribute objects using the isEqual: method. Because the default implementation of this method checks only the existing properties of this class, you must implement your own version of the method to compare any additional properties. If your custom properties are all equal, call super and return the resulting value at the end of your implementation.

Tasks

Creating Layout Attributes

Identifying the Referenced Item

Accessing the Layout Attributes

Properties

alpha

The transparency of the item.

@property (nonatomic) CGFloat alpha;
Discussion

Possible values are between 0.0 (transparent) and 1.0 (opaque). The default is 1.0.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

bounds

The bounds of the item.

@property (nonatomic) CGRect bounds;
Discussion

When setting the bounds, the origin of the bounds rectangle must always be at (0, 0). Changing the bounds rectangle also changes the value in the size property to match the new bounds size.

Availability
  • Available in iOS 7.0 and later.
Declared In
UICollectionViewLayout.h

center

The center point of the item.

@property (nonatomic) CGPoint center;
Discussion

The center point is specified in the coordinate system of the collection view. Setting the value of this property also updates the origin of the rectangle in the frame property.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

frame

The frame rectangle of the item.

@property (nonatomic) CGRect frame;
Discussion

The frame rectangle is measured in points and specified in the coordinate system of the collection view. Setting the value of this property also sets the values of the center and size properties.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

hidden

Determines whether the item is currently displayed.

@property (nonatomic, getter=isHidden) BOOL hidden;
Discussion

The default value of this property is NO. As an optimization, the collection view might not create the corresponding view if this property is set to YES.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

indexPath

The index path of the item in the collection view.

@property (nonatomic, retain) NSIndexPath *indexPath;
Discussion

The index path contains the index of the section and the index of the item within that section. These two values uniquely identify the position of the corresponding item in the collection view.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

representedElementCategory

The type of the item. (read-only)

@property (nonatomic, readonly) UICollectionElementCategory representedElementCategory;
Discussion

You can use the value in this property to distinguish whether the layout attributes are intended for a cell, supplementary view, or decoration view.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

representedElementKind

The layout-specific identifier for the target view. (read-only)

@property (nonatomic, readonly) NSString *representedElementKind;
Discussion

You can use the value in this property to identify the specific purpose of the supplementary or decoration view associated with the attributes. This property is nil if the representedElementCategory property contains the value UICollectionElementCategoryCell.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

size

The size of the item.

@property (nonatomic) CGSize size;
Discussion

Setting the value of this property also changes the size of the rectangle returned by the frame and boundsproperties.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

transform

The affine transform of the item

@property (nonatomic) CGAffineTransform transform
Discussion

Assigning a transform other than the identity transform to this property causes the frame property to be set to CGRectNull. Assigning a value also replaces the value in the transform3D property with a 3D version of the affine transform you specify.

Availability
  • Available in iOS 7.0 and later.
Declared In
UICollectionViewLayout.h

transform3D

The 3D transform of the item.

@property (nonatomic) CATransform3D transform3D;
Discussion

Assigning a transform other than the identity transform to this property causes the frame property to be set to CGRectNull. Assigning a value also replaces the value in the transform property with an affine version of the 3D transform you specify.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

zIndex

Specifies the item’s position on the z axis.

@property (nonatomic) NSInteger zIndex;
Discussion

This property is used to determine the front-to-back ordering of items during layout. Items with higher index values appear on top of items with lower values. Items with the same value have an undetermined order.

The default value of this property is 0.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

Class Methods

layoutAttributesForCellWithIndexPath:

Creates and returns a layout attributes object that represents a cell with the specified index path.

+ (instancetype)layoutAttributesForCellWithIndexPath:(NSIndexPath *)indexPath
Parameters
indexPath

The index path of the cell.

Return Value

A new layout attributes object whose precise type matches the type of the class used to call this method.

Discussion

Use this method to create a layout attributes object for a cell in the collection view. Cells are the main type of view presented by a collection view. The index path for a cell typically includes both a section index and an item index for locating the cell’s contents in the collection view’s data source.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

layoutAttributesForDecorationViewOfKind:withIndexPath:

Creates and returns a layout attributes object that represents the specified decoration view.

+ (instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath*)indexPath
Parameters
decorationViewKind

The kind identifier for the specified decoration view.

indexPath

An index path related to the decoration view.

Return Value

A new layout attributes object whose precise type matches the type of the class used to call this method.

Discussion

Use this method to create a layout attributes object for a decoration view in the collection view. Decoration views are a type of supplementary view but do not present data that is managed by the collection view’s data source. Instead, they mostly present visual adornments for a section or for the entire collection view.

It is up to you to decide how to use the indexPath parameter to identify a given decoration view. Typically, you use the decorationViewKind parameter to identify the type of the decoration view and the indexPath information to distinguish between different instances of that view.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

layoutAttributesForSupplementaryViewOfKind:withIndexPath:

Creates and returns a layout attributes object that represents the specified supplementary view.

+ (instancetype)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind withIndexPath:(NSIndexPath *)indexPath
Parameters
elementKind

A string that identifies the type of supplementary view.

indexPath

The index path of the view.

Return Value

A new layout attributes object whose precise type matches the type of the class used to call this method.

Discussion

Use this method to create a layout attributes object for a supplementary view in the collection view. Like cells, supplementary views present data that is managed by the collection view’s data source. But unlike cells, supplementary views are typically designed for a special purpose. For example, header and footer views are laid out differently than cells and can be provided for individual sections or for the collection view as a whole.

It is up to you to decide how to use the indexPath parameter to identify a given supplementary view. Typically, you use the elementKind parameter to identify the type of the supplementary view and the indexPath information to distinguish between different instances of that view.

Availability
  • Available in iOS 6.0 and later.
Declared In
UICollectionViewLayout.h

Constants

UICollectionElementCategory

Constants specifying the type of view.

typedef enum {
   UICollectionElementCategoryCell,
   UICollectionElementCategorySupplementaryView,
   UICollectionElementCategoryDecorationView
} UICollectionElementCategory;
Constants
UICollectionElementCategoryCell

The view is a cell.

Available in iOS 6.0 and later.

Declared in UICollectionViewLayout.h.

UICollectionElementCategorySupplementaryView

The view is a supplementary view.

Available in iOS 6.0 and later.

Declared in UICollectionViewLayout.h.

UICollectionElementCategoryDecorationView

The view is a decoration view.

Available in iOS 6.0 and later.

Declared in UICollectionViewLayout.h.