Marco Monsanto
Marco Monsanto

Reputation: 13

Give margin to one specific cell, UICollectionVIew

I got a problem and I cant find an answer on stackoverflow and in google to this one, I got a UICollectionVIew with this format:

 _______
|xxxxxxx|
| xxxxx |
| xxxxx |
| xxxxx |
| xxxxx |
|_______|

and I want to give a margin in the first cell to get something like this:

 _______
|xxxxxxx|
|       |
| xxxxx |
| xxxxx |
| xxxxx |
|_______|

I have tried to give margin to the cell but that gives the margin to every single cell and its not what I want, just for that single cell with indexPath.item == 0.

is that possible? if not what can I do to achieve that layout? thank you in advance

Upvotes: 0

Views: 367

Answers (2)

user3820674
user3820674

Reputation: 262

You should make two collection view cell type. One wider with some free space at bottom. And second for others.

Upvotes: 0

Sam Meadley
Sam Meadley

Reputation: 227

Are you using UICollectionViewFlowLayout? If so I'd recommend making use of a header view for your first cell:

 _______
|xxxxxxx|

and reserve UICollectionViewCell for the remaining items:

| xxxxx |
| xxxxx |
| xxxxx |
|_______|

UICollectionView supports three different types of items;

  • Cells
  • Supplementary Views
  • Decoration Views

UICollectionViewFlowLayout provides two ready-to-use Supplementary Views, a header view and a footer view. The header view sounds perfect for your needs here.

Once you've implemented the header views, the margin you describe can be achieved by adjusting the UICollectionViewFlowLayout sectionInset property. Or implementing the delegate method:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;


There's a bunch of material on the web describing Header/Footer views;

http://www.appcoda.com/supplementary-view-uicollectionview-flow-layout/

I also recommend the Collection View Programming Guide for iOS - it's a long but excellent read. Especially if you ever need to configure any custom layouts.

Hope that helps.

Upvotes: 1

Related Questions