Skip to content

Instantly share code, notes, and snippets.

@BenJamminnn
Created July 14, 2015 20:39
Show Gist options
  • Save BenJamminnn/f07964a6817e2b5c3fce to your computer and use it in GitHub Desktop.
Save BenJamminnn/f07964a6817e2b5c3fce to your computer and use it in GitHub Desktop.
PageViewController with UICollectionViewControllers
import UIKit
class ViewController: UIPageViewController, UIPageViewControllerDataSource {
var collectionViewControllers: Array<UIViewController> = []
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self //so our delegate methods get called
//use tags to reference each controller
let collectionViewOne = TestCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
collectionViewOne.view.tag = 0
let collectionViewTwo = TestCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
collectionViewTwo.view.tag = 1
let collectionViewThree = TestCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
collectionViewThree.view.tag = 2
collectionViewControllers = [collectionViewOne, collectionViewTwo, collectionViewThree]
self.setViewControllers([collectionViewOne], direction: .Forward, animated: true, completion: nil)
}
//dataSource methods
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var index = viewController.view.tag
if index == 0 {
return nil
}
if index > 0 {
index--
}
return collectionViewControllers[index] as? TestCollectionViewController
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var index = viewController.view.tag
if index == 2 {
return nil
}
if index < collectionViewControllers.count - 1 {
index++
}
return collectionViewControllers[index] as? TestCollectionViewController
}
}
import UIKit
class TestCollectionViewController: UICollectionViewController {
private let reuseIdentifier = "collectionViewCell"
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.backgroundColor = UIColor.whiteColor()
self.collectionView?.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func randomColor() -> UIColor {
let randomInt = arc4random_uniform(4)
var randomColor = UIColor.blackColor()
switch randomInt {
case 0:
randomColor = UIColor.purpleColor()
case 1:
randomColor = UIColor.redColor()
case 2:
randomColor = UIColor.greenColor()
case 3:
randomColor = UIColor.cyanColor()
default:
break
}
return randomColor
}
}
extension TestCollectionViewController: UICollectionViewDataSource {
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
cell.backgroundColor = randomColor()
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment