|
| 1 | +// |
| 2 | +// ExampleViewController.swift |
| 3 | +// SwipingViewController |
| 4 | +// |
| 5 | +// Created by Oscar Apeland on 12.10.2017. |
| 6 | +// Copyright © 2017 Tise. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +class ExampleViewController: UIViewController { |
| 12 | + lazy var collectionView: UICollectionView = { |
| 13 | + $0.backgroundColor = .white |
| 14 | + $0.alwaysBounceVertical = true |
| 15 | + $0.delegate = self |
| 16 | + $0.dataSource = self |
| 17 | + $0.autoresizingMask = [.flexibleHeight, .flexibleWidth] |
| 18 | + ($0.collectionViewLayout as? UICollectionViewFlowLayout)?.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0) |
| 19 | + |
| 20 | + return $0 |
| 21 | + }(UICollectionView(frame: view.bounds, collectionViewLayout: UICollectionViewFlowLayout())) |
| 22 | + |
| 23 | + let cellColor = UIColor.random |
| 24 | + |
| 25 | + override func viewDidLoad() { |
| 26 | + super.viewDidLoad() |
| 27 | + view.addSubview(collectionView) |
| 28 | + if #available(iOS 11.0, *) { |
| 29 | + collectionView.contentInsetAdjustmentBehavior = .always |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +extension ExampleViewController: UICollectionViewDataSource, UICollectionViewDelegate { |
| 35 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 36 | + return 100 |
| 37 | + } |
| 38 | + |
| 39 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 40 | + collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") |
| 41 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) |
| 42 | + cell.backgroundColor = cellColor |
| 43 | + |
| 44 | + return cell |
| 45 | + } |
| 46 | + |
| 47 | + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| 48 | + let vc = UICollectionViewController(collectionViewLayout: UICollectionViewFlowLayout()) |
| 49 | + |
| 50 | + vc.collectionView?.backgroundColor = .white |
| 51 | + vc.collectionView?.dataSource = self |
| 52 | + vc.collectionView?.delegate = self |
| 53 | + |
| 54 | + navigationController?.pushViewController(vc, animated: true) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +extension UIColor { |
| 59 | + class var random: UIColor { |
| 60 | + func random() -> CGFloat { |
| 61 | + return CGFloat(arc4random()) / CGFloat(UInt32.max) |
| 62 | + } |
| 63 | + |
| 64 | + return UIColor(red: random(), green: random(), blue: random(), alpha: 1.0) |
| 65 | + } |
| 66 | +} |
0 commit comments