Class: UICollectionViewLayout

Inherits:
NSObject show all

Overview

The UICollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view. The job of a layout object is to determine the placement of cells, supplementary views, and decoration views inside the collection view’s bounds and to report that information to the collection view when asked. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.

Direct Known Subclasses

UICollectionViewFlowLayout

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Instance Attribute Details

- (UICollectionView) collectionView (readonly)

The collection view object currently using this layout object. (read-only) The collection view object sets the value of this property when a new layout object is assigned to it.

Returns:

Class Method Details

+ (Class) layoutAttributesClass

Returns the class to use when creating layout attributes objects. If you subclass UICollectionViewLayoutAttributes in order to manage additional layout attributes, you should override this method and return your custom subclass. The methods for creating layout attributes use this class when creating new layout attributes objects.This method is intended for subclassers only and does not need to be called by your code.

Returns:

  • (Class)

    The class to use for layout attributes objects.

Instance Method Details

- (CGSize) collectionViewContentSize

Returns the width and height of the collection view’s contents. Subclasses must override this method and use it to return the width and height of the collection view’s content. These values represent the width and height of all the content, not just the content that is currently visible. The collection view uses this information to configure its own content size for scrolling purposes. The default implementation of this method returns CGSizeZero.

Returns:

  • (CGSize)

    The width and height of the collection view’s contents.

- (Object) finalizeAnimatedBoundsChange

Cleans up after any animated changes to the view’s bounds or after the insertion or deletion of items. The collection view calls this method after creating the animations for changing the view’s bounds or after the animated insertion or deletion of items. This method is the layout object’s opportunity to do any cleanup related to those operations. You can also use this method to perform additional animations. Any animations you create are added to the animation block used to handle the insertions, deletions, and bounds changes.

Returns:

- (Object) finalizeCollectionViewUpdates

Performs any additional animations or clean up needed during a collection view update. The collection view calls this method as the last step before preceding to animate any changes into place. This method is called within the animation block used to perform all of the insertion, deletion, and move animations so you can create additional animations using this method as needed. Otherwise, you can use it to perform any last minute tasks associated with managing your layout object’s state information.

Returns:

- (UICollectionViewLayoutAttributes) finalLayoutAttributesForDisappearingDecorationElementOfKind(elementKind, atIndexPath:elementIndexPath)

Returns the final layout information for a decoration view that is about to be removed from the collection view. This method is called after the prepareForCollectionViewUpdates: method and before the finalizeCollectionViewUpdates method for any decoration views that are about to be deleted. Your implementation should return the layout information that describes the final position and state of the view. The collection view uses this information as the end point for any animations. (The starting point of the animation is the view’s current location.) If you return nil, the layout object uses the same attributes for both the start and end points of the animation.The default implementation of this method returns nil.

Parameters:

  • elementKind (String)

    A string that identifies the type of decoration view.

  • elementIndexPath (NSIndexPath)

    The index path of the view being deleted.

Returns:

  • (UICollectionViewLayoutAttributes)

    A layout attributes object that describes the position of the decoration view to use as the end point for animating its removal.

- (UICollectionViewLayoutAttributes) finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath)

Returns the final layout information for an item that is about to be removed from the collection view. This method is called after the prepareForCollectionViewUpdates: method and before the finalizeCollectionViewUpdates method for any items that are about to be deleted. Your implementation should return the layout information that describes the final position and state of the item. The collection view uses this information as the end point for any animations. (The starting point of the animation is the item’s current location.) If you return nil, the layout object uses the same attributes for both the start and end points of the animation.The default implementation of this method returns nil.

Parameters:

  • itemIndexPath (NSIndexPath)

    The index path of the item being deleted.

Returns:

- (UICollectionViewLayoutAttributes) finalLayoutAttributesForDisappearingSupplementaryElementOfKind(elementKind, atIndexPath:elementIndexPath)

Returns the final layout information for a supplementary view that is about to be removed from the collection view. This method is called after the prepareForCollectionViewUpdates: method and before the finalizeCollectionViewUpdates method for any supplementary views that are about to be deleted. Your implementation should return the layout information that describes the final position and state of the view. The collection view uses this information as the end point for any animations. (The starting point of the animation is the view’s current location.) If you return nil, the layout object uses the same attributes for both the start and end points of the animation.The default implementation of this method returns nil.

Parameters:

  • elementKind (String)

    A string that identifies the type of supplementary view.

  • elementIndexPath (NSIndexPath)

    The index path of the view being deleted.

Returns:

  • (UICollectionViewLayoutAttributes)

    A layout attributes object that describes the position of the supplementary view to use as the end point for animating its removal.

- (UICollectionViewLayoutAttributes) initialLayoutAttributesForAppearingDecorationElementOfKind(elementKind, atIndexPath:elementIndexPath)

Returns the starting layout information for a decoration view being inserted into the collection view. This method is called after the prepareForCollectionViewUpdates: method and before the finalizeCollectionViewUpdates method for any decoration views that are about to be inserted. Your implementation should return the layout information that describes the initial position and state of the view. The collection view uses this information as the starting point for any animations. (The end point of the animation is the view’s new location in the collection view.) If you return nil, the layout object uses the item’s final attributes for both the start and end points of the animation.The default implementation of this method returns nil.

Parameters:

  • elementKind (String)

    A string that identifies the type of decoration view.

  • elementIndexPath (NSIndexPath)

    The index path of the item being inserted.

Returns:

- (UICollectionViewLayoutAttributes) initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath)

Returns the starting layout information for an item being inserted into the collection view. This method is called after the prepareForCollectionViewUpdates: method and before the finalizeCollectionViewUpdates method for any items that are about to be inserted. Your implementation should return the layout information that describes the initial position and state of the item. The collection view uses this information as the starting point for any animations. (The end point of the animation is the item’s new location in the collection view.) If you return nil, the layout object uses the item’s final attributes for both the start and end points of the animation.The default implementation of this method returns nil.

Parameters:

  • itemIndexPath (NSIndexPath)

    The index path of the item being inserted. You can use this path to locate the item in the collection view’s data source.

Returns:

- (UICollectionViewLayoutAttributes) initialLayoutAttributesForAppearingSupplementaryElementOfKind(elementKind, atIndexPath:elementIndexPath)

Returns the starting layout information for a supplementary view being inserted into the collection view. This method is called after the prepareForCollectionViewUpdates: method and before the finalizeCollectionViewUpdates method for any supplementary views that are about to be inserted. Your implementation should return the layout information that describes the initial position and state of the view. The collection view uses this information as the starting point for any animations. (The end point of the animation is the view’s new location in the collection view.) If you return nil, the layout object uses the item’s final attributes for both the start and end points of the animation.The default implementation of this method returns nil.

Parameters:

  • elementKind (String)

    A string that identifies the type of supplementary view.

  • elementIndexPath (NSIndexPath)

    The index path of the item being inserted.

Returns:

- (Object) invalidateLayout

Invalidates the current layout and triggers a layout update. You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.

Returns:

- (UICollectionViewLayoutAttributes) layoutAttributesForDecorationViewOfKind(decorationViewKind, atIndexPath:indexPath)

Returns the layout attributes for the specified decoration view. If your layout object defines any decoration views, you must override this method and use it to return layout information for those views.

Parameters:

  • decorationViewKind (String)

    A string that identifies the type of the decoration view.

  • indexPath (NSIndexPath)

    The index path of the decoration view.

Returns:

- (Array) layoutAttributesForElementsInRect(rect)

Returns the layout attributes for all of the cells and views in the specified rectangle. Subclasses must override this method and use it to return layout information for all items whose view intersects the specified rectangle. Your implementation should return attributes for all visual elements, including cells, supplementary views, and decoration views. When creating the layout attributes, always create an attributes object that represents the correct element type (cell, supplementary, or decoration). The collection view differentiates between attributes for each type and uses that information to make decisions about which views to create and how to manage them.

Parameters:

  • rect (CGRect)

    The rectangle (specified in the collection view’s coordinate system) containing the target views.

Returns:

- (UICollectionViewLayoutAttributes) layoutAttributesForItemAtIndexPath(indexPath)

Returns the layout attributes for the item at the specified index path. Subclasses must override this method and use it to return layout information for items in the collection view. You use this method to provide layout information only for items that have a corresponding cell. Do not use it for supplementary views or decoration views.

Parameters:

  • indexPath (NSIndexPath)

    The index path of the item.

Returns:

- (UICollectionViewLayoutAttributes) layoutAttributesForSupplementaryViewOfKind(kind, atIndexPath:indexPath)

Returns the layout attributes for the specified supplementary view. If your layout object defines any supplementary views, you must override this method and use it to return layout information for those views.

Parameters:

  • kind (String)

    A string that identifies the type of the supplementary view.

  • indexPath (NSIndexPath)

    The index path of the view.

Returns:

- (Object) prepareForAnimatedBoundsChange(oldBounds)

Prepares the layout object for animated changes to the view’s bounds or the insertion or deletion of items. The collection view calls this method before performing any animated changes to the view’s bounds or before the animated insertion or deletion of items. This method is the layout object’s opportunity to perform any calculations needed to prepare for those animated changes. Specifically, you might use this method to calculate the initial or final positions of inserted or deleted items so that you can return those values when asked for them.You can also use this method to perform additional animations. Any animations you create are added to the animation block used to handle the insertions, deletions, and bounds changes.

Parameters:

  • oldBounds (CGRect)

    The current bounds of the collection view.

Returns:

- (Object) prepareForCollectionViewUpdates(updateItems)

Prepares the layout object to receive changes to the contents of the collection view. When items are inserted or deleted, the collection view notifies its layout object so that it can adjust the layout as needed. The first step in that process is to call this method to let the layout object know what changes to expect. After that, additional calls are made to gather layout information for inserted, deleted, and moved items that are going to be animated around the collection view.

Parameters:

Returns:

- (Object) prepareLayout

Tells the layout object to update the current layout. Layout updates occur the first time the collection view presents its content and whenever the layout is invalidated explicitly or implicitly because of a change to the view. During each layout update, the collection view calls this method first to give your layout object a chance to prepare for the upcoming layout operation. The default implementation of this method does nothing. Subclasses can override it and use it to set up data structures or perform any initial computations needed to perform the layout later.

Returns:

- (Object) registerClass(viewClass, forDecorationViewOfKind:decorationViewKind)

Registers a class for use in creating decoration views for a collection view. This method gives the layout object a chance to register a decoration view for use in the collection view. Decoration views provide visual adornments to a section or to the entire collection view but are not otherwise tied to the data provided by the collection view’s data source.You do not need to create decoration views explicitly. After registering one, it is up to the layout object to decide when a decoration view is needed and return the corresponding layout attributes from its layoutAttributesForElementsInRect: method. For layout attributes that specify a decoration view, the collection view creates (or reuses) a view and displays it automatically based on the registered information. If you previously registered a class or nib file with the same kind string, the class you specify in the viewClass parameter replaces the old entry. You may specify nil for viewClass if you want to unregister the decoration view.

Parameters:

  • viewClass (Class)

    The class to use for the supplementary view.

  • decorationViewKind (String)

    The element kind of the decoration view. You can use this string to distinguish between decoration views with different purposes in the layout. This parameter must not be nil and must not be an empty string.

Returns:

- (Object) registerNib(nib, forDecorationViewOfKind:decorationViewKind)

Registers a nib file for use in creating decoration views for a collection view. This method gives the layout object a chance to register a decoration view for use in the collection view. Decoration views provide visual adornments to a section or to the entire collection view but are not otherwise tied to the data provided by the collection view’s data source.You do not need to create decoration views explicitly. After registering one, it is up to the layout object to decide when a decoration view is needed and return the corresponding layout attributes from its layoutAttributesForElementsInRect: method. For layout attributes that specify a decoration view, the collection view creates (or reuses) a view and displays it automatically based on the registered information. If you previously registered a class or nib file with the same kind string, the class you specify in the viewClass parameter replaces the old entry. You may specify nil for viewClass if you want to unregister the decoration view.

Parameters:

  • nib (UINib)

    The nib object containing the cell definition. The nib file must contain only one top-level object and that object must be of the type UICollectionReusableView.

  • decorationViewKind (String)

    The element kind of the decoration view. You can use this string to distinguish between decoration views with different purposes in the layout. This parameter must not be nil and must not be an empty string.

Returns:

- (Boolean) shouldInvalidateLayoutForBoundsChange(newBounds)

Asks the layout object if the new bounds require a layout update. The default implementation of this method returns NO. Subclasses should override it and return an appropriate value based on whether changes in the bounds of the collection view require changes to the layout of cells and supplementary views.

Parameters:

  • newBounds (CGRect)

    The new bounds of the collection view.

Returns:

  • (Boolean)

    YES if the collection view requires a layout update or NO if the layout does not need to change.

- (CGPoint) targetContentOffsetForProposedContentOffset(proposedContentOffset, withScrollingVelocity:velocity)

Returns the point at which to stop scrolling. If you want the scrolling behavior to snap to specific boundaries, you can override this method and use it to change the point at which to stop. For example, you might use this method to always stop scrolling on a boundary between items, as opposed to stopping in the middle of an item.

Parameters:

  • proposedContentOffset (CGPoint)

    The proposed point (in the collection view’s content view) at which to stop scrolling. This is the value at which scrolling would naturally stop if no adjustments were made. The point reflects the upper-left corner of the visible content.

  • velocity (CGPoint)

    The current scrolling velocity along both the horizontal and vertical axes. This value is measured in points per second.

Returns:

  • (CGPoint)

    The content offset that you want to use instead. This value reflects the adjusted upper-left corner of the visible area. The default implementation of this method returns the value in the proposedContentOffset parameter.