Created
January 14, 2019 16:45
-
-
Save jslinker/05751918c8f0990039ab648633bddd86 to your computer and use it in GitHub Desktop.
UICollectionViewControllerFlowLayout boiler plate. Just fill out the `prepare` function with you layout logic and the rest just works.
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
class SampleFlowLayout: UICollectionViewLayout { | |
override func prepare() { | |
guard let collectionView = self.collectionView else { return } | |
self.itemAttributes.removeAllObjects() | |
// go through every section.. | |
for section in 0..<collectionView.numberOfSections { | |
let numberOfItems = collectionView.numberOfItems(inSection: section) | |
// ..and every item | |
for item in 0..<numberOfItems { | |
// create the frame attribute for the index path | |
let attributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: item, section: section)) | |
attributes.frame = self.frameForItemNumber(itemNumber, isBelowRibbon: isBelowRibbon) | |
attributes.zIndex = 1 | |
self.itemAttributes.add(attributes) | |
} | |
} | |
// save the last frame | |
let lastAtt = self.itemAttributes.lastObject as? UICollectionViewLayoutAttributes | |
// calculate the total content size | |
if (lastAtt != nil) { | |
let maxCellY = lastAtt!.frame.maxY | |
self.contentSize = CGSize(width: self.collectionView!.frame.width, height: maxCellY + bottomPadding) | |
} | |
else { | |
self.contentSize = CGSize.zero | |
} | |
} | |
// MARK: Flow Layout Overrides | |
override var collectionViewContentSize: CGSize { | |
return self.contentSize | |
} | |
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | |
// hand back the attributes that are in the rect | |
let filteredArray = self.itemAttributes.filtered(using: NSPredicate(block: { (object, binding) -> Bool in | |
let attObject = object as! UICollectionViewLayoutAttributes | |
return rect.intersects(attObject.frame) | |
})) as! [UICollectionViewLayoutAttributes] | |
return filteredArray | |
} | |
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { | |
return newBounds.width != self.collectionView?.frame.width || newBounds.height != self.collectionView?.frame.height | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment