Skip to content

Instantly share code, notes, and snippets.

@miguelfermin
Created May 8, 2017 10:05
Show Gist options
  • Save miguelfermin/21c2332a8a1f164116f6fb618e719c22 to your computer and use it in GitHub Desktop.
Save miguelfermin/21c2332a8a1f164116f6fb618e719c22 to your computer and use it in GitHub Desktop.
Demonstrates how to add a UIPageViewController to a UIViewController
//
// ViewController.swift
// PageViewController
//
// Created by Miguel Fermin on 5/8/17.
// Copyright © 2017 MAF Software LLC. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var viewControllers: [UIViewController]!
var pageViewController: UIPageViewController!
override func viewDidLoad() {
super.viewDidLoad()
setupPageController()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func setupPageController() {
pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageViewController.delegate = self
viewControllers = [
storyboard!.instantiateViewController(withIdentifier: "one"),
storyboard!.instantiateViewController(withIdentifier: "two"),
storyboard!.instantiateViewController(withIdentifier: "three"),
storyboard!.instantiateViewController(withIdentifier: "four")
]
pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .forward, animated: true, completion: nil)
pageViewController.dataSource = self
addChildViewController(pageViewController)
view.addSubview(pageViewController.view)
pageViewController!.view.frame = view.bounds
pageViewController.didMove(toParentViewController: self)
// Add the page view controller's gesture recognizers to the view controller's view so that the gestures are started more easily.
view.gestureRecognizers = pageViewController.gestureRecognizers
}
}
// MARK: - UIPageViewController DataSource and Delegate
extension ViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = indexOfViewController(viewController)
if (index == 0) || (index == NSNotFound) {
return nil
}
index -= 1
return viewControllerAtIndex(index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = indexOfViewController(viewController)
if index == NSNotFound {
return nil
}
index += 1
if index == viewControllers.count {
return nil
}
return viewControllerAtIndex(index)
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return viewControllers.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
}
// MARK: - Helpers
extension ViewController {
fileprivate func viewControllerAtIndex(_ index: Int) -> UIViewController? {
if viewControllers.count == 0 || index >= viewControllers.count {
return nil
}
return viewControllers[index]
}
fileprivate func indexOfViewController(_ viewController: UIViewController) -> Int {
return viewControllers.index(of: viewController) ?? NSNotFound
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment