Skip to content
This repository was archived by the owner on Mar 26, 2019. It is now read-only.

Dynamic item size, disable pinning #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CustomCollectionLayout/CollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
53 changes: 22 additions & 31 deletions CustomCollectionLayout/CustomCollectionViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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..<collectionView.numberOfSections {

for item in 0..<collectionView.numberOfItems(inSection: section) {
if section != 0 && item != 0 {
continue
}

let attributes = layoutAttributesForItem(at: IndexPath(item: item, section: section))!
if section == 0 {
if shouldPinFirstRow && section == 0 {
var frame = attributes.frame
frame.origin.y = collectionView.contentOffset.y
attributes.frame = frame
}

if item == 0 {
if shouldPinFirstColumn && item == 0 {
var frame = attributes.frame
frame.origin.x = collectionView.contentOffset.x
attributes.frame = frame
Expand Down Expand Up @@ -86,10 +98,7 @@ class CustomCollectionViewLayout: UICollectionViewLayout {
extension CustomCollectionViewLayout {

func generateItemAttributes(collectionView: UICollectionView) {
if itemsSize.count != numberOfColumns {
calculateItemSizes()
}


var column = 0
var xOffset: CGFloat = 0
var yOffset: CGFloat = 0
Expand All @@ -101,7 +110,7 @@ extension CustomCollectionViewLayout {
var sectionAttributes: [UICollectionViewLayoutAttributes] = []

for index in 0..<numberOfColumns {
let itemSize = itemsSize[index]
let itemSize = sizeForItem(column: index, section: section)
let indexPath = IndexPath(item: index, section: section)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = CGRect(x: xOffset, y: yOffset, width: itemSize.width, height: itemSize.height).integral
Expand Down Expand Up @@ -149,28 +158,10 @@ extension CustomCollectionViewLayout {
}
}

func calculateItemSizes() {
itemsSize = []

for index in 0..<numberOfColumns {
itemsSize.append(sizeForItemWithColumnIndex(index))
}
}

func sizeForItemWithColumnIndex(_ columnIndex: Int) -> 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)
}

}
2 changes: 2 additions & 0 deletions CustomCollectionLayout/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
</dict>
</plist>