When the UICollectionView is populated with items they always go right to the edges of the UICollectionView like so:
---------------
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
---------------
I would like to place a margin around each edge like so:
---------------
| X X X X X X |
| X X X X X X |
| X X X X X X |
| X X X X X X |
| X X X X X X |
| X X X X X X |
---------------
I tried to achieve this by placing the UICollectionView inside its hosting UIView by setting its Frame to simulate a border but it scrolls within the Frame so gets cut off at the top and bottom and the scroller also appears in the bounds of the frame.
I have looked at the API but I cannot see anything obvious to achieve this. Any ideas?
You can make use of the following function.
You will have to play around with the number to figure out how to force the collectionviewCells in a single line.
For Swift 3
I know this is an old question, but don't forget that UICollectionView inherits from UIScrollView.
Be aware that both contentInset and sectionInset may change the spacing of cells in your view. For more information on that, see this post.
In
you can give your cells some margin on the left side.
Or you can create custom cells, which have a margin.
or you can set the property .sectionInset of your
CollectionviewFlowLayout
, which should be the easiest way (if you use FlowLayout)Swift 3 version (Vinayak Kini answer) :
On Xcode 8, if you're using the interface builder you can set the Section Insets right on the Size Inspector: