-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColumnFlowLayout.swift
43 lines (32 loc) · 1.57 KB
/
ColumnFlowLayout.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// ColumnFlowLayout.swift
// iOS Coding
//
// Created by Vishnu M P on 06/07/21.
//
import UIKit
class ColumnFlowLayout: UICollectionViewFlowLayout {
let numberOfItemsInRow: Int
init(_ numberOfItemsInRow: Int, minimumInteritemSpacing: CGFloat = 0, minimumLineSpacing: CGFloat = 0, sectionInset: UIEdgeInsets = .zero) {
self.numberOfItemsInRow = numberOfItemsInRow
super.init()
self.minimumInteritemSpacing = minimumInteritemSpacing
self.minimumLineSpacing = minimumLineSpacing
self.sectionInset = sectionInset
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepare() {
super.prepare()
guard let collectionView = collectionView else { return }
let marginsAndInsets = sectionInset.left + sectionInset.right + collectionView.safeAreaInsets.left + collectionView.safeAreaInsets.right + minimumInteritemSpacing * CGFloat(numberOfItemsInRow - 1)
let itemWidth = ((collectionView.bounds.size.width - marginsAndInsets) / CGFloat(numberOfItemsInRow)).rounded(.down)
itemSize = CGSize(width: itemWidth, height: collectionView.frame.height / 2)
}
override func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext {
let context = super.invalidationContext(forBoundsChange: newBounds) as! UICollectionViewFlowLayoutInvalidationContext
context.invalidateFlowLayoutDelegateMetrics = newBounds.size != collectionView?.bounds.size
return context
}
}