Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gopinath0332/f857e821bb82708c7d682d942e8dd16f to your computer and use it in GitHub Desktop.
Save gopinath0332/f857e821bb82708c7d682d942e8dd16f to your computer and use it in GitHub Desktop.
DSSCollectionViewFlowLayout - A UICollectionViewFlow that will equally space UICollectionView Cells
//
// DSSCollectionViewFlowLayout.swift
//
// Created by Gary L. Wade on 9/5/16. Adapted by Rob Lester on 5/2/18.
// Copyright © 2018 Gary L. Wade. All rights reserved.
// Original: https://medium.com/@garywade/equally-spacing-uicollectionview-cells-6d74401f8f56
//
// Tested On: Xcode 9.3 / Swift 4.1
//
import UIKit
@objc(DSSCollectionViewFlowLayout)
class DSSCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func prepare () {
super.prepare()
var contentByItems: ldiv_t
let contentSize = self.collectionViewContentSize
let itemSize = self.itemSize
if UICollectionViewScrollDirection.vertical == self.scrollDirection {
contentByItems = ldiv (Int(contentSize.width), Int(itemSize.width))
} else {
contentByItems = ldiv (Int(contentSize.height), Int(itemSize.height))
}
let layoutSpacingValue = CGFloat(NSInteger (CGFloat(contentByItems.rem))) / CGFloat (contentByItems.quot + 1)
let originalMinimumLineSpacing = self.minimumLineSpacing
let originalMinimumInteritemSpacing = self.minimumInteritemSpacing
let originalSectionInset = self.sectionInset
if layoutSpacingValue != originalMinimumLineSpacing ||
layoutSpacingValue != originalMinimumInteritemSpacing ||
layoutSpacingValue != originalSectionInset.left ||
layoutSpacingValue != originalSectionInset.right ||
layoutSpacingValue != originalSectionInset.top ||
layoutSpacingValue != originalSectionInset.bottom {
let insetsForItem = UIEdgeInsets.init(top: layoutSpacingValue, left: layoutSpacingValue, bottom: layoutSpacingValue, right: layoutSpacingValue)
self.minimumLineSpacing = layoutSpacingValue
self.minimumInteritemSpacing = layoutSpacingValue
self.sectionInset = insetsForItem
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment