I have horizontal CollectionView and when i start the app last image in my gallery was only half-displayed. How to fix that?
Asked
Active
Viewed 2,862 times
2
-
The contentView frame is probably bigger than the CollectionView frame. Can you provide some code? – alephao Nov 12 '15 at 17:38
-
I have to set the size of the cells that have to do with the width of the screen. What code do you need, I have the standard codes for collectionView. I do not know how to do that. – Milos Mandic Nov 13 '15 at 10:12
-
Check this answer - https://stackoverflow.com/a/45459605/5032981 – Prashant Gaikwad Jan 16 '18 at 15:12
3 Answers
3
I have faced similar issue and what helped me is to set minimumInteritemSpacingForSectionAt and minimumLineSpacingForSectionAt
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}

Vitalik Kizlov
- 385
- 5
- 9
2
The contentView frame is probably bigger than the CollectionView frame.
Let's say you want the collectionView to display 5 cells...
On viewDidLoad:
let cvWidth = collectionView.frame.width
let cvHeight = collectionView.frame.height
let cellEdge = cvWidth / 5
let contentSize = cellEdge * CGFloat(numberOfCells)
collectionView.contentSize = CGSize(width: contentSize, height: cvHeight)
Don't forget to set your numberOfCells value
If you have some space between cells, you should set cellEdge this way:
let cellEdge = cvWidth / 5 + space
Edit - about UICollectionViewDelegateFlowLayout:
To set the space between cells and sections first add the UICollectionViewFlowLayoutDelegate to the ViewController:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
than you can set the interspace, line spacing and inset via this functions:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
-
-
You have an array of images right? You can set numberOfCells = arrayOfImage.count @JoeDominguez – alephao Nov 14 '15 at 17:53
-
You can read about cell spacing and UICollectionViewFlowLayout here: http://stackoverflow.com/questions/28325277/how-to-set-cell-spacing-and-uicollectionview-uicollectionviewflowlayout-size-r @JoeDominguez – alephao Nov 14 '15 at 17:55
-
-
still do not know how to set space, i tried whit let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3) layout.minimumInteritemSpacing = 0 layout.minimumLineSpacing = 0 – Milos Mandic Nov 14 '15 at 19:40
2
The easiest solution it so add some bottom "inset" value.
To do that
- In your xcode's "utility area" click on "size inspector" tab
- Then refer the image below

Ariven Nadar
- 1,278
- 13
- 13