Or Centre UICollectionView Cells Horizontally iOS for US dudes.

I had to do this for a project recently so thought I would share.

Create a single view project in Objective C and open the Storyboard. For ease of this example turn Auto Layout and Size Classes off using the iPad size for the view. This can be done in File Inspector View on the right.

Screen Shot 2015-04-28 at 21.44.02

In the general settings under Deployment info, un-tick the boxes for portrait and upside down so the app can only be displayed horizontally.

Next, setup the View Controller simulated metrics in the Attribute Inspector as follows.

Screen Shot 2015-04-28 at 21.44.25

Add a UICollectionView to the ViewController and make it 1000 in width and 300 in height. Try to centre it in the middle of the View.

Screen Shot 2015-04-28 at 21.47.50

 

Change the scroll direction to horizontal in the Attribute Inspector for the UICollectionView. Make sure items is set to 1 while you are there so that a prototype cell stays in the collection.

Screen Shot 2015-04-28 at 21.58.06

Now make the CollectionViewCell 200 in width and 250 in height

Screen Shot 2015-04-28 at 21.50.14

I would now add some views and labels to the default prototype cell so that what you are working with is realistic. Here is my collection View and cell.

Screen Shot 2015-04-28 at 22.01.40

With the UICollectionView selected, go into Connections Inspector and connect the Delegate to the View Controller.

Screen Shot 2015-04-28 at 22.04.38

We need to implement the UICollectionViewDelegate. Add this to the ViewController.h

 

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UICollectionViewDelegate>

@end

Now we will add two methods from the UICollectionViewDelegate. These methods will simply tell the UICollectionView how many collection cells we’re displaying and what cells to display. Pretty much the same as a tableView Delegate.

#import "ViewController.h"

@implementation ViewController
{
	NSInteger _numberOfCells;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	_numberOfCells = 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

	return _numberOfCells;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

	UICollectionViewCell *cell = (UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

	return cell;
}

Now build and run the project. You should see something similar to the below. If not, put a breakpoint in the delegate methods and make sure they are called and the delegate is setup correctly.

Screen Shot 2015-05-04 at 20.16.07

Now lets add another method to centre the 3 UICollectionViewCells. You will notice I have hardcoded in all of the width values but that is just to make this more understandable, it should be quite simple to get the values at run time programmatically. So you should now adapt your ViewController to look like the below.

#import "ViewController.h"

#define CELL_WIDTH 200
#define CELL_SPACING 10
#define COLLECTIONVIEW_WIDTH 1000

@implementation ViewController
{
	NSInteger _numberOfCells;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	_numberOfCells = 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

	return _numberOfCells;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

	UICollectionViewCell *cell = (UICollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

	return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
	NSInteger viewWidth = COLLECTIONVIEW_WIDTH;
	NSInteger totalCellWidth = CELL_WIDTH * _numberOfCells;
	NSInteger totalSpacingWidth = CELL_SPACING * (_numberOfCells -1);

	NSInteger leftInset = (viewWidth - (totalCellWidth + totalSpacingWidth)) / 2;
	NSInteger rightInset = leftInset;

	return UIEdgeInsetsMake(0, leftInset, 0, rightInset);
}

@end

There is no reason to use that many lines of code or to have both a left and right inset other than simplicity for this demonstration. That method can be reduced to a few lines at most. Run the project again and you should have a centred UICollectionView like below.

Screen Shot 2015-05-05 at 20.26.22