Reputation: 1071
I have horizontal CollectionView and when i start the app last image in my gallery was only half-displayed. How to fix that?
Upvotes: 2
Views: 3091
Reputation: 384
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
}
Upvotes: 3
Reputation: 1308
The easiest solution it so add some bottom "inset" value.
To do that
Upvotes: 2
Reputation: 1273
The contentView frame is probably bigger than the CollectionView frame.
Let's say you want the collectionView to display 5 cells...
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
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
}
Upvotes: 2