【UIKit】 Creating a PageViewController with Code Only

Swift Basics

In this post, I’ll show how to implement a PageViewController using only code. This is a record of my work as of January 29, 2023.

What is a PageViewController?

A PageViewController allows you to create page-turning animations, mimicking the effect of flipping through pages in a book.

Implementation

Setting Up a Code-Only Project

To avoid using Storyboards, refer to this blog post for setting up a project where everything is implemented in code: How to Set Up a Code-Only Swift Project.

Declaring the UIPageViewController and ViewControllers

First, we need to declare our PageViewController and an array to hold the view controllers that will be displayed in the pages:

private var pageViewController: UIPageViewController!
private var controllers: [UIViewController] = []

Implementing the DataSource Methods

We then implement the data source methods to handle the number of pages and the logic for navigating between them.

// MARK: - UIPageViewController DataSource
extension ViewController: UIPageViewControllerDataSource {

// Number of pages
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return self.controllers.count
}

// Swipe left (next page)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let index = self.controllers.firstIndex(of: viewController), index < self.controllers.count - 1 {
return self.controllers[index + 1]
} else {
return nil
}
}

// Swipe right (previous page)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let index = self.controllers.firstIndex(of: viewController), index > 0 {
return self.controllers[index - 1]
} else {
return nil
}
}
}

Configuring the PageViewController

Next, we define a function to configure the basic settings of the PageViewController.

private func initPageViewController() {
// Background colors for the pages
let backColor: [UIColor] = [.systemIndigo, .systemOrange, .systemGreen]

// Create and store view controllers
for i in 0...2 {
let myViewController: UIViewController = UIViewController()
myViewController.view.backgroundColor = backColor[i]
myViewController.view.frame = self.view.frame
self.controllers.append(myViewController)
}

// Configure the PageViewController
pageViewController = UIPageViewController(
transitionStyle: .pageCurl, // Animation for turning the page
navigationOrientation: .horizontal, // Page turn direction (horizontal or vertical)
options: nil
)

// Set the initial view controller
pageViewController.setViewControllers([self.controllers[0]], direction: .forward, animated: true, completion: nil)
pageViewController.dataSource = self

// Add the PageViewController to the parent view controller
self.addChild(self.pageViewController)
self.view.addSubview(self.pageViewController.view!)
}

Final Thoughts

I plan to update this post as needed with more detailed explanations and settings. I hope this helps someone out there! See you next time.

Comments

Copied title and URL