Skip to content
This repository was archived by the owner on Oct 18, 2022. It is now read-only.

Commit b3c3ce2

Browse files
author
Anna-Mariia Shkarlinska
committed
Add possibility to customize cell size
1 parent ae1d0e2 commit b3c3ce2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

CarLensCollectionViewLayout/CarLensCollectionViewLayout.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ public final class CarLensCollectionViewLayout: UICollectionViewFlowLayout {
4040
guard let collectionView = collectionView else { return }
4141
scrollDirection = .horizontal
4242
minimumLineSpacing = options.minimumSpacing
43-
itemSize = CGSize(width: collectionView.bounds.width - 60, height: collectionView.bounds.height)
43+
itemSize = options.itemSize ?? CGSize(width: collectionView.bounds.width - 60, height: collectionView.bounds.height)
4444
let sidesInset = (collectionView.bounds.width - itemSize.width) / 2
45-
collectionView.contentInset = .init(top: 0, left: sidesInset, bottom: 0, right: sidesInset)
45+
let topAndBottomInset = (collectionView.bounds.height - itemSize.height) / 2
46+
collectionView.contentInset = .init(top: topAndBottomInset, left: sidesInset, bottom: topAndBottomInset, right: sidesInset)
4647
collectionView.decelerationRate = options.decelerationRate
4748
collectionView.showsHorizontalScrollIndicator = options.shouldShowScrollIndicator
4849
}

CarLensCollectionViewLayout/CarLensCollectionViewLayoutOptions.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
/// The optional configuration of CarLensCollectionViewLayout.
10+
/// The optional configuration of CarLensCollectionViewLayout. Use this if you need to customize `CarLensCollectionViewLayout`.
1111
public struct CarLensCollectionViewLayoutOptions {
1212

1313
/// A minimum spacing between cells.
@@ -19,16 +19,22 @@ public struct CarLensCollectionViewLayoutOptions {
1919
/// A value indicating whether collection view should have a scroll indicator.
2020
let shouldShowScrollIndicator: Bool
2121

22+
/// The size to use for cells.
23+
let itemSize: CGSize?
24+
2225
/// The initialization of the optional layout configuration.
26+
/// You can initialize it with any of the parameters available. Others will be configured automatically.
2327
///
2428
/// - Parameters:
2529
/// - minimumSpacing: A minimum spacing between cells. The default value is `20`.
2630
/// - decelerationRate: A deceleration for a scroll view. The default value is `.fast`.
2731
/// - shouldShowScrollIndicator: A value indicating whether collection view should have a scroll indicator. The default value is `false`.
28-
public init(minimumSpacing: CGFloat = 20, decelerationRate: UIScrollView.DecelerationRate = UIScrollView.DecelerationRate.fast, shouldShowScrollIndicator: Bool = false) {
32+
/// - itemSize: The size to use for cells. The default height is equal to a collection view height. The width is equal to the `collection view width - 60`.
33+
public init(minimumSpacing: CGFloat = 20, decelerationRate: UIScrollView.DecelerationRate = UIScrollView.DecelerationRate.fast, shouldShowScrollIndicator: Bool = false, itemSize: CGSize? = nil) {
2934
self.minimumSpacing = minimumSpacing
3035
self.decelerationRate = decelerationRate
3136
self.shouldShowScrollIndicator = shouldShowScrollIndicator
37+
self.itemSize = itemSize
3238
}
3339

3440
}

0 commit comments

Comments
 (0)