In iOS applications, we use the container and content View Controllers to display the content of the application. However, there are requirements for navigation between the View Controllers in the application so that the user can easily switch between the view controllers. In this section of the tutorial, we will discuss the PageViewControlller is the container view controller that manages the navigation between the pages of the content where each page is managed by a child view controller. It is the instance of UIPageViewController, which inherits UIViewController.
The PageViewController is used in many of the iOS applications to let the user navigate between the various pages of the content of the application. The navigation can be controlled programmatically in the application. The PageViewController uses the transition that we specify to animate the change.
In this example, we will create a project with two View Controllers where a root page view controller manages the navigation between the View Controllers.
First, we need to add the PageViewController to the storyboard. For this purpose, search for Page View Controller in the object library and drag the result to the storyboard.
This will add the Page View Controller in the storyboard, as shown in the following image. We will set this View Controller as the initial view controller in the attribute inspector and also assign RootPageViewController.swift and storyboard id to RootVC in the Identity inspector.
Since the PageViewController we have added used to manage the navigation between different view controllers. Hence, in this project, we will also add two View Controllers, as shown in the following image. We have given the different background color to the View Controllers to identify them. We have also given the storyboard id to the view controllers as FirstVC and SecondVC in the identity inspector.
Here, we have built the storyboard for the project. Now, we will programmatically define the navigation in the RootPageViewController class. It conforms to the UIPageViewControllerDataSource protocol and implements its two methods to return the view controller that will be present before and after to the current view controllers.
The RootPageViewController class defines the list of View Controllers among which the navigation takes place. The first view controller present in the list is set as the current view controller using the setViewControllers() method of PageViewController class.