0

I have a horizontal UICollectionView which works fine. But I want to center cell when there is no need for scrolling. i.e.

  1. if all cell are able to fit within the view width & user don't need to scroll then Do center the cell
  2. if all cell are not able to fit within the view width & user need to scroll then Don't center the cell (default behaviour)

Graphical example:

  1. UICollectionView that needs scrolling then keep default behavior

enter image description here

  1. UICollectionView that doesn't need scrolling

enter image description here

What I have tried so far:

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    var dataList:[String] = ["icon_rotate", "icon_rotate2", "icon_rotate3", "icon_rotate4", "icon_rotate5", "icon_rotate6"]

 
    override func viewDidLoad() {
       super.viewDidLoad()
       //other stuff
    }

    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
      return (UIDevice.current.userInterfaceIdiom == .pad) ? 20.0 : 10.0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
      return (UIDevice.current.userInterfaceIdiom == .pad) ? 20.0 : 10.0
    }



    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       return CGSize(width: collectionView.frame.size.height * 0.75, height: collectionView.frame.size.height * 0.75)
     }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return dataList.count
     }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "editorCell", for: indexPath) as! EditorCell
    
    cell.iconView.image = UIImage(named: dataList[indexPath.row])
    
    
    return cell
   }

}

This results in default behavior of UICollectionView so I searched in SO and found this post: How to center horizontally UICollectionView Cells? and I added this code:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

  let totalCellWidth = CellWidth * CellCount
  let totalSpacingWidth = CellSpacing * (CellCount - 1)

  let leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
  let rightInset = leftInset

  return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}

This code does work fine when there's few cells to be loaded but I want this to dynamically detect if centering is needed or let the default behavior stay in action.

Thanks

1 Answers1

1

Assuming your current code is working when you the cells will fit - that is, it centers the "row of cells" when desired - change this line:

let leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2

to:

let leftInset = max(0, (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2.0)

That way your insets will never be less than Zero

DonMag
  • 44,662
  • 5
  • 32
  • 56