Javatpoint Logo
Javatpoint Logo

PageViewController

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.

UIPageViewControllers Properties

SN Property Description
1 var dataSource: UIPageViewControllerDataSource? It is the object of the UIPageViewControllerDataSource protocol that contains the method to provide view controllers to navigate.
2 protocol UIPageViewControllerDataSource The protocol UIPageViewControllerDataSource provides the view controllers on demand of the application in response to navigation gestures.
3 var delegate: UIPageViewControllerDelegate? It is the delegate object of the Page View Controller.
4 protocol UIPageViewControllerDelegate This protocol contains the delegate methods that are notified when the device orientation changes and when the user navigates to a new page.
5 var viewControllers: [UIViewController]? It is the list of UIViewControllers that are displayed by the page view controller.
6 var gestureRecognizers: [UIGestureRecognizer] The list of Gesture Recognizer objects that are configured to handle user interaction.
7 var navigationOrientation: UIPageViewController.NavigationOrientation It is the direction of navigation.
8 var spineLocation: UIPageViewController.SpineLocation It represents the location of the spine.
9 var transitionStyle: UIPageViewController.TransitionStyle It is the style used for transition between the view controllers.
10 var isDoubleSided: Bool It is a Boolean value that indicates whether the content is displayed on the backside of the pages.

UIPageViewController Functions

SN Method Description
1 func setViewControllers([UIViewController]?, direction: UIPageViewController.NavigationDirection, animated: Bool, completion: ((Bool) -> Void)?) This method is used to set the view controllers to be displayed by the page view controller.

Example

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.

Main.storyboard

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.

iOS PageViewController

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.

iOS PageViewController

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.

iOS PageViewController

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.

RootPageViewController.swift

Output:

iOS PageViewController




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA