iOS Developer Library

Developer

UIKit Framework Reference UICollectionViewLayoutAttributes Class Reference

Options
Deployment Target:

On This Page
Language:

UICollectionViewLayoutAttributes

Inherits From


  • NSObject
  • UICollectionViewLayoutAttributes

Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 6.0 and later

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.

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

    Declaration

    Swift

    convenience init(forCellWithIndexPath indexPath: NSIndexPath)

    Objective-C

    + (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

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

    Declaration

    Swift

    convenience init(forSupplementaryViewOfKind elementKind: String, withIndexPath indexPath: NSIndexPath)

    Objective-C

    + (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

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

    Declaration

    Swift

    convenience init(forDecorationViewOfKind decorationViewKind: String, withIndexPath indexPath: NSIndexPath)

    Objective-C

    + (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • indexPath indexPath Property

    The index path of the item in the collection view.

    Declaration

    Swift

    var indexPath: NSIndexPath!

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The type of the item. (read-only)

    Declaration

    Swift

    var representedElementCategory: UICollectionElementCategory { get }

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

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

    Declaration

    Swift

    var representedElementKind: String! { get }

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • frame frame Property

    The frame rectangle of the item.

    Declaration

    Swift

    var frame: CGRect

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • bounds bounds Property

    The bounds of the item.

    Declaration

    Swift

    var bounds: CGRect

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later

  • center center Property

    The center point of the item.

    Declaration

    Swift

    var center: CGPoint

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • size size Property

    The size of the item.

    Declaration

    Swift

    var size: CGSize

    Objective-C

    @property(nonatomic) CGSize size

    Discussion

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • The 3D transform of the item.

    Declaration

    Swift

    var transform3D: CATransform3D

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • transform transform Property

    The affine transform of the item

    Declaration

    Swift

    var transform: CGAffineTransform

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later

  • alpha alpha Property

    The transparency of the item.

    Declaration

    Swift

    var alpha: CGFloat

    Objective-C

    @property(nonatomic) CGFloat alpha

    Discussion

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • zIndex zIndex Property

    Specifies the item’s position on the z axis.

    Declaration

    Swift

    var zIndex: Int

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • hidden hidden Property

    Determines whether the item is currently displayed.

    Declaration

    Swift

    var hidden: Bool

    Objective-C

    @property(nonatomic, getter=isHidden) BOOL hidden

    Discussion

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later

  • Constants specifying the type of view.

    Declaration

    Swift

    enum UICollectionElementCategory : UInt { case Cell case SupplementaryView case DecorationView }

    Objective-C

    typedef enum { UICollectionElementCategoryCell, UICollectionElementCategorySupplementaryView, UICollectionElementCategoryDecorationView } UICollectionElementCategory;

    Constants

    • Cell

      UICollectionElementCategoryCell

      The view is a cell.

      Available in iOS 6.0 and later

    • SupplementaryView

      UICollectionElementCategorySupplementaryView

      The view is a supplementary view.

      Available in iOS 6.0 and later

    • DecorationView

      UICollectionElementCategoryDecorationView

      The view is a decoration view.

      Available in iOS 6.0 and later

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later