Created
May 2, 2018 18:43
-
-
Save rlester/2a56a138b1c1a799eae33b7e67cadc5a to your computer and use it in GitHub Desktop.
DSSCollectionViewFlowLayout - A UICollectionViewFlow that will equally space UICollectionView Cells
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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