Javatpoint Logo
Javatpoint Logo


Till now, we have discussed CollectionView, which is a type of content view used to represent the collection of items where the UICollectionViewCell represents an item. We have created iOS applications using the CollectionViews. In this section of the tutorial, we will use CollectionViewController to manage the collectionview.

The CollectionViewController can be defined as the ViewController, which is specialized in managing the CollectionView. It is an instance of UICollectionViewController, which inherits the UIViewController class.

Like TableViewController, we cannot add any custom UIView to the CollectionView controller as it can only manage the CollectionView. The CollectionViewController contains an in-built collectionview in the storyboard with at least one collectionview cell (item). However, we can have as many items in the CollectionViewController as we want. We can access the collectionview in the UICollectionViewController subclass by using the collectionView property of the class.

The CollectionViewController implements UICollectionViewDelegate and UICollectionViewDataSource protocol to control the data and user interactions of the CollectionView. When the CollectionView is about to appear for the first time, the CollectionViewController reloads the collection view data. It also clears the current selection every time the view is displayed. However, we can change this behavior by setting the value of clearsSelectionOnViewWillAppear property to false.

Adding CollectionViewController to the interface

Create a new Single View iOS application in XCode. We will get the following auto-generated files in XCode.

iOS CollectionViewController

Here, we will need to delete the existing View Controller in the storyboard and add the Collection View Controller by searching for it in the object library and drag the result to the storyboard.

iOS CollectionViewController

This will create a Collection View Controller in the storyboard with a collection view cell.

iOS CollectionViewController

In our project, we also need to create the subclass of UICollectionViewController, which can be assigned to the Collection View Controller in the storyboard.

To create a new class file, press Command + n and select the option "Cocoa Touch Class".

iOS CollectionViewController

This will open a window shown in the following image. Enter the name of the class and select the parent class as UICollectioViewController from the dropdown.

iOS CollectionViewController

Here, we have created a class MyCollectionViewController, which is a subclass of UICollectionViewController. Assign this class to the collection view controller in the storyboard.

CollectionViewController Components

Collection View Controller contains various specific components that are visible on the screen.

iOS CollectionViewController
iOS CollectionViewController

CollectionView: The Collection View Controller provides an in-built collection view with a collection view cell. This collectionview can be accessed by using collectionView property in the UICollectionViewController subclass. It is an instance of the UICollectionView class which inherits UIView.

CollectionViewCell: The Collection View Cell displays the actual content of the Collection View Controller. It contains a content view of type UIView to which we can add the custom subviews. It is an instance of the UICollectionViewCell class.

NavigationBar: Navigation Bar is shown on the top of the ViewControllers that are embedded in the navigation controllers. It contains title and bar button items. It is an instance of the UINavigationBar class.

UICollectionViewController Properties

The UICollectionViewController contains the following properties.

SN Property Description
1 var collectionView: UICollectionView! It represents the collection view object managed by the View Controller.
2 var collectionViewLayout: UICollectionViewLayout It represents the layout object used to initialize the collection view controller.
3 var clearsSelectionOnViewWillAppear: Bool It is a boolean value indicating whether the selection should be cleared when the view is about to appear on the screen.
4 var installsStandardGestureForInteractiveMovement: Bool A Boolean value indicating whether the collection view controller installs a standard gesture recognizer to drive the reordering process.
5 var useLayoutToLayoutNavigationTransitions: Bool A Boolean that indicates whether the collection view controller coordinates with a navigation controller for transitions.


In this example, we will create a simple Collection View Controller, and we will assign different colors to each collectionview cell.

Interface Builder

Here, we will search for the collection view controller in the object library and drag the result to the storyboard. This will create a CollectionViewController in the storyboard with the collectionview prototype cell.

iOS CollectionViewController

Now, we will create a subclass of UICollectionViewController and name this class as MyCollectionViewController.



iOS CollectionViewController

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA