Question: Description:
------------

Answer for `Objective-C` and `Swift2.0`: [How to center align the cells of a UICollectionView?](https://stackoverflow.com/questions/13588283/how-to-center-align-the-cells-of-a-uicollectionview) 

I usually would try to convert the `Swift2.0` answer to the `Swift3.0` solution, however the method:

```
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        let edgeInsets = (screenWight - (CGFloat(elements.count) * 50) - (CGFloat(elements.count) * 10)) / 2
        return UIEdgeInsetsMake(0, edgeInsets, 0, 0);
    }

```

doesn't seem to exist in `Swift3.0`,and the only other method I found that seems useful is:

```
func collectionView(_ collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
        <#code#>
}

```

but I am unsure how to implement it correctly.

Question:
---------

How to center align the cells of a `UICollectionView` in `Swift3.0`?

(A simple and general solution for iOS and tvOS would be perfect)

Answer: 
The method that you are looking for is present with a different signature in `Swift 3`. The new signature is this:

```
optional public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets

```

**P.S:** This method is present in `UICollectionViewDelegateFlowLayout` protocol.  

Hope this would help.
```
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

let allCellWidth = KcellWidth * numberOfCell
let cellSpacingWidth = CellSpacing * (numberOfCell - 1)

let leftInset = (collectionViewWidth - CGFloat(allCellWidth + cellSpacingWidth)) / 2
let rightInset = leftInset

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

```