iOS Developer Library

Developer

UIKit Framework Reference UICollectionViewController Class Reference

Options
Deployment Target:

On This Page
Language:

UICollectionViewController

Inheritance


Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 6.0 and later.

The UICollectionViewController class represents a view controller whose content consists of a collection view. It implements the following behavior:

  • If the collection view controller has an assigned nib file or was loaded from a storyboard, it loads its view from the corresponding nib file or storyboard. If you create the collection view controller programmatically, it automatically creates a new unconfigured collection view object, which you can access using the collectionView property.

  • When loading a collection view from a storyboard or nib file, the data source and delegate objects for the collection view are obtained from the nib file. If a data source or delegate is not specified, the collection view controller assigns itself to the unspecified role.

  • When the collection view is about to appear for the first time, the collection view controller reloads the collection view data. It also clears the current selection every time the view is displayed. You can change this behavior by setting the value of the clearsSelectionOnViewWillAppear property to NOfalse.

You create a custom subclass of UICollectionViewController for each collection view that you want to manage. When you initialize the controller, using the initWithCollectionViewLayout: method, you specify the layout the collection view should have. Because the initially created collection view is without dimensions or content, the collection view’s data source and delegate—typically the collection view controller itself—must provide this information.

You may override the loadView method or any other superclass method, but if you do, be sure to call super in the implementation of your method. If you do not, the collection view controller may not be able to perform all of the tasks needed to maintain the integrity of the collection view.

  • Initializes a collection view controller and configures the collection view with the provided layout.

    Declaration

    Swift

    init(collectionViewLayout layout: UICollectionViewLayout)

    Objective-C

    - (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout

    Parameters

    layout

    The layout object to associate with the collection view. The layout controls how the collection view presents its cells and supplementary views.

    Return Value

    An initialized UICollectionViewController object or nil if the object could not be created.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later.

  • The collection view object managed by this view controller.

    Declaration

    Swift

    var collectionView: UICollectionView?

    Objective-C

    @property(nonatomic, retain) UICollectionView *collectionView

    Discussion

    If you assign a new collection view object to this property and that view’s data source or delegate are not yet set, the collection view controller makes itself the delegate or data source or both.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later.

  • The layout object used to initialize the collection view controller. (read-only)

    Declaration

    Swift

    var collectionViewLayout: UICollectionViewLayout! { get }

    Objective-C

    @property(nonatomic, readonly) UICollectionViewLayout *collectionViewLayout

    Discussion

    This property contains the layout object you passed to the initWithCollectionViewLayout: method. The layout object in this property is not updated to reflect changes to the collection view itself. You can use this property to refer to the layout object you originally configured the collection view to use.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • A Boolean value indicating if the controller clears the selection when the collection view appears.

    Declaration

    Swift

    var clearsSelectionOnViewWillAppear: Bool

    Objective-C

    @property(nonatomic) BOOL clearsSelectionOnViewWillAppear

    Discussion

    The default value of this property is YEStrue. When YEStrue, the collection view controller clears the collection view’s current selection when it receives a viewWillAppear: message. Setting this property to NOfalse preserves the selection.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later.

  • A Boolean that indicates whether the collection view controller coordinates with a navigation controller for transitions.

    Declaration

    Swift

    var useLayoutToLayoutNavigationTransitions: Bool

    Objective-C

    @property(nonatomic, assign) BOOL useLayoutToLayoutNavigationTransitions

    Discussion

    This property helps facilitate transitions between two or more collection view controllers using a navigation controller. When configuring your navigation controller, install a collection view controller as the root object on the navigation stack and set its value for this property to NOfalse. When the user selects an item that would require pushing a new collection view controller on the stack, set the value of this property for the new view controller to YEStrue. When you do that, the navigation controller performs an animated layout change between the contents of the two collection view controllers instead of the traditional push animation. Similarly, popping the topmost collection view controller off the stack animates back to the previous layout. The navigation controller drives the transition between the view controllers, including the ability to drive the transition interactively.

    You must set the value of this property before pushing the collection view controller onto a navigation stack. Do not change the value of this property after the view controller is already on the navigation stack.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.