2

I have horizontal CollectionView and when i start the app last image in my gallery was only half-displayed. How to fix that?

Milos Mandic
  • 960
  • 2
  • 11
  • 19

3 Answers3

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
  • 290
  • 3
  • 8
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
}
Community
  • 1
  • 1
alephao
  • 1,044
  • 10
  • 18
  • How to set space and numberOfCells? – Milos Mandic Nov 14 '15 at 17:48
  • 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
  • and how to set Space? – Milos Mandic 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
1

The easiest solution it so add some bottom "inset" value.

To do that

  1. In your xcode's "utility area" click on "size inspector" tab
  2. Then refer the image below

xcode's ScrollView

Ariven Nadar
  • 1,108
  • 11
  • 13