Skip to content

Instantly share code, notes, and snippets.

@tolleiv
Created June 29, 2018 15:41
Show Gist options
  • Save tolleiv/4be00409af8672a6e7287ff12e0267a1 to your computer and use it in GitHub Desktop.
Save tolleiv/4be00409af8672a6e7287ff12e0267a1 to your computer and use it in GitHub Desktop.
import UIKit
class CustomPageViewController: UIPageViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {
fileprivate lazy var pages: [UIViewController] = {
return [
self.getViewController(withIdentifier: "firstVc"),
self.getViewController(withIdentifier: "secondVc")
]
}()
fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = pages.index(of: viewController) else { return nil }
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else { return pages.last }
guard pages.count > previousIndex else { return nil }
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = pages.index(of: viewController) else { return nil }
let nextIndex = viewControllerIndex + 1
guard nextIndex < pages.count else { return pages.first }
guard pages.count > nextIndex else { return nil }
return pages[nextIndex]
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firstVC = pages.first
{
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
}
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
//corrects scrollview frame to allow for full-screen view controller pages
for subView in self.view.subviews {
if subView is UIScrollView {
subView.frame = self.view.bounds
}
}
super.viewDidLayoutSubviews()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment