Created
February 1, 2018 10:14
-
-
Save tigerraj32/8516b90becb76579ae1c103f2f4dbb05 to your computer and use it in GitHub Desktop.
Playing with CollectionViewController in playground
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
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
class MyCollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { | |
fileprivate let itemsPerRow = 3 | |
fileprivate let sectionInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.title = "Collection View Tutorial" | |
self.collectionView?.backgroundColor = UIColor.red | |
self.collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") | |
self.collectionView?.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") | |
} | |
override func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 5 | |
} | |
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return 5 | |
} | |
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) | |
cell.contentView.backgroundColor = UIColor.gray | |
cell.contentView.layer.cornerRadius = 10 | |
cell.contentView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner] | |
return cell | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
let paddingSpace = sectionInsets.left * CGFloat(itemsPerRow + 1) | |
let width = self.view.frame.width - paddingSpace | |
let widthPerItem = width / CGFloat(itemsPerRow) | |
return CGSize(width: widthPerItem, height: widthPerItem) | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
return sectionInsets | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { | |
return sectionInsets.left | |
} | |
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { | |
// return CGSize(width: 300, height: 100) | |
// } | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { | |
return CGSize(width: self.view.frame.width, height: 50) | |
} | |
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { | |
print(kind) | |
var resuableView: UICollectionReusableView? = nil | |
if kind == UICollectionElementKindSectionHeader { | |
resuableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) | |
//resuableView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50) | |
resuableView?.backgroundColor = UIColor.green | |
} | |
return resuableView! | |
} | |
} | |
let nav = UINavigationController(rootViewController: MyCollectionVC(collectionViewLayout: UICollectionViewFlowLayout())) | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = nav |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment