diff --git a/CustomCollectionLayout/CollectionViewController.swift b/CustomCollectionLayout/CollectionViewController.swift index 9117f33..1757d74 100644 --- a/CustomCollectionLayout/CollectionViewController.swift +++ b/CustomCollectionLayout/CollectionViewController.swift @@ -70,6 +70,22 @@ extension CollectionViewController: UICollectionViewDataSource { } +extension CollectionViewController: CustomCollectionViewLayoutDelegate { + + func collectionView(_ collectionView: UICollectionView, widthOfColumn column: Int) -> CGFloat { + return 90 + } + + func collectionView(_ collectionView: UICollectionView, heightOfSection section: Int) -> CGFloat { + return 48 + } + + func numberOfColumns(in collectionView: UICollectionView) -> Int { + return 8 + } + +} + // MARK: - UICollectionViewDelegate extension CollectionViewController: UICollectionViewDelegate { diff --git a/CustomCollectionLayout/CustomCollectionViewLayout.swift b/CustomCollectionLayout/CustomCollectionViewLayout.swift index 0c78ad4..8395c47 100644 --- a/CustomCollectionLayout/CustomCollectionViewLayout.swift +++ b/CustomCollectionLayout/CustomCollectionViewLayout.swift @@ -8,15 +8,24 @@ import UIKit +protocol CustomCollectionViewLayoutDelegate: class { + func collectionView(_ collectionView: UICollectionView, widthOfColumn column: Int) -> CGFloat + func collectionView(_ collectionView: UICollectionView, heightOfSection section: Int) -> CGFloat +} + class CustomCollectionViewLayout: UICollectionViewLayout { - let numberOfColumns = 8 + var numberOfColumns = 0 var shouldPinFirstColumn = true var shouldPinFirstRow = true var itemAttributes = [[UICollectionViewLayoutAttributes]]() var itemsSize = [CGSize]() var contentSize: CGSize = .zero + + weak var delegate: CustomCollectionViewLayoutDelegate? { + return collectionView?.delegate as? CustomCollectionViewLayoutDelegate + } override func prepare() { guard let collectionView = collectionView else { @@ -27,25 +36,28 @@ class CustomCollectionViewLayout: UICollectionViewLayout { return } + numberOfColumns = collectionView.numberOfItems(inSection: 0) + if itemAttributes.count != collectionView.numberOfSections { generateItemAttributes(collectionView: collectionView) return } - + for section in 0.. CGSize { - var text: NSString - - switch columnIndex { - case 0: - text = "MMM-99" - - default: - text = "Content" - } - - let size: CGSize = text.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)]) - let width: CGFloat = size.width + 16 - return CGSize(width: width, height: 30) + func sizeForItem(column: Int, section: Int) -> CGSize { + let width = delegate?.collectionView(collectionView!, widthOfColumn: column) ?? 0 + let height = delegate?.collectionView(collectionView!, heightOfSection: section) ?? 0 + return CGSize(width: width, height: height) } } diff --git a/CustomCollectionLayout/Info.plist b/CustomCollectionLayout/Info.plist index 61861ab..92443e9 100644 --- a/CustomCollectionLayout/Info.plist +++ b/CustomCollectionLayout/Info.plist @@ -31,6 +31,8 @@ UISupportedInterfaceOrientations UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeRight + UIInterfaceOrientationLandscapeLeft