Milos Mandic
Milos Mandic

Reputation: 1071

Last horizontal collectionview cell is half displeyed

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

Answers (3)

Vitalik  Kizlov
Vitalik Kizlov

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

Ariven Nadar
Ariven Nadar

Reputation: 1308

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

Upvotes: 2

alephao
alephao

Reputation: 1273

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
}

Upvotes: 2

Related Questions