6
votes

I'm looking to use the following function but unfortunately it's not being called.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlowLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        let cellCount = Double(collectionView.numberOfItems(inSection: section))
        if let cell = collectionView.cellForItem(at: IndexPath(row: 0, section: section))
        {
            let cellWidth = Double(cell.frame.size.width)
            let totalCellWidth = cellWidth * cellCount
            let cellSpacing = Double(collectionViewLayout.minimumInteritemSpacing)
            let totalSpacingWidth = cellSpacing * (cellCount - 1)

            let leftInset = (collectionView.frame.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
            let rightInset = leftInset

            return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
        }

    return collectionView.layoutMargins
}

I'm getting the following console errors but my cell is smaller than my collectionView and I've set the section insets and layout margins to 0.

2017-03-30 11:25:14.624 Prep[92400:1862856] The behavior of the UICollectionViewFlowLayout is not defined because: 2017-03-30 11:25:14.624 Prep[92400:1862856] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 2017-03-30 11:25:14.624 Prep[92400:1862856] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {0, 68.5}> collection view layout: . 2017-03-30 11:25:14.625 Prep[92400:1862856] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger. cell height --> 42.0

enter image description here

Protocols

class OverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vipCollectionView: UICollectionView!

var hostedMeetingsArray: [String]?
var vipArray: [Attendee]?

class OverviewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vipCollectionView: UICollectionView!

var hostedMeetingsArray: [String]?
var vipArray: [Attendee]?

enter image description here

1
Try to give the collection view a constant height and test if it is working. Maybe the height constraint makes the problem. - Kingalione
@Kingalione This gets rid of the console errors (I was using a proportional height constraint) but the function is still not called. - Declan McKenna
Did you implemented the correct delegate? - Kingalione
@Kingalione I've updated the question with the protocols used. I used UICollectionViewDelegate, UICollectionViewDataSource, and UICollectionViewDelegateFlowLayout. The last of these I have declared at the top of the class and nothing more. - Declan McKenna
yes, its the correct protocol. So is cellForItemAt and numberOfItemsInSection is getting called? - Kingalione

1 Answers

2
votes

Ok, the problem that insetForSectionAt is not called is that you have to implement it like this:

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

   //access to minimumInteritemSpacing by casting to UICollectionViewFlowLayout
   let layout = collectionViewLayout as! UICollectionViewFlowLayout
   let spacing = layout.minimumInteritemSpacing       

   return UIEdgeInsetsMake(0.5,0,0.5,0)

}

Take a look on UICollectionViewDelegateFlowLayout protocol. The insetForSectionAt is declared like this:

@available(iOS 6.0, *)
optional public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets

So you need that first optional parameter, to implement this function.