7

I want to add some space before the first cell in a collection view, something like an offset, my collection view has a horizontal scroll position.

Here is my current collection view:

current

It has a leading constraint of 35, what I want is the cell to start at an "x" position of 35 but with the ability to scroll full width like in this:

desired

Is there a way to create that initial offset in a collection view using Swift 3?

Jonathan Solorzano
  • 5,994
  • 16
  • 57
  • 119
  • 1
    It may help you https://stackoverflow.com/questions/28325277/how-to-set-cell-spacing-and-uicollectionview-uicollectionviewflowlayout-size-r – Dharma Jun 21 '17 at 15:41

6 Answers6

8

You could just set Section Insets for Collection View Flow Layout without code.

enter image description here

Community
  • 1
  • 1
6

Swift 5 / Xcode 11

Thanks Alexander Spirichev.

Based on his answer, you can also set the left inset to 35 points programmatically:

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 8
layout.sectionInset = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 0)

collectionView.setCollectionViewLayout(layout, animated: false)
A.Kant
  • 1,958
  • 1
  • 16
  • 15
1

You need to set contentInset

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(top, left, bottom, right);
}

you can see in detail here

QuangLoc
  • 61
  • 4
1

You can set space before the initial cell in collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) as follow:

if (indexPath.row == 0 || indexPath.row == 1) {
  // Set value according to user requirements
  cell.frame.origin.y = 10
}
H S Progr
  • 3,035
  • 18
  • 24
0

A bit late to the party here, but I came across a couple solutions

The right way (scroll to first cell):

https://coderwall.com/p/e-ajeq/uicollectionview-set-initial-contentoffset

Simple hacky way: I add in a transparent "spacer" cell to the first item to offset the rest of the cells.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)

    if (indexPath.row == 0) {
        cell.backgroundColor = UIColor.white
        return cell
    }

    cell.backgroundColor = UIColor.green
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if (indexPath.row == 0){
        //spacer
        return CGSize(width:6, height:50)
    }
    return CGSize(width: 50, height: 50)
}
Blakedallen
  • 571
  • 1
  • 7
  • 22
0

If you only want the cells in that section to have the padding, then use sectionInset from UICollectionViewFlowLayout as suggested in the other answers. But from what I can guess you're probably fine with instead setting contentInsets on the collection view itself (inherited from UIScrollView)

DeFrenZ
  • 2,068
  • 1
  • 20
  • 19