TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

iOS | PageViewController

iOS | PageViewController with XCode IDE Introduction, History and Versions, Views and View Controllers, Creating the first iOS application, Label, Button, TextField, Switch, Segmented Control, iOS UI Controls, iOS UI Views, iOS UIView Controllers, Tab Bar Interface etc.

<< Back to IOS

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.

class UIPageViewController : 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

import UIKit


class RootPageViewController: UIPageViewController {
    
    lazy var vcList:[UIViewController] = {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let firstVC = storyboard.instantiateViewController(identifier: "FirstVC")
        let secondVC = storyboard.instantiateViewController(identifier: "SecondVC")
        
        return [firstVC, secondVC]
        
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.dataSource = self
        if let vc = vcList.first{
            self.setViewControllers([vc], direction: .forward, animated: true, completion: nil)
        }
        
    }
}


extension RootPageViewController : UIPageViewControllerDataSource{
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let index = vcList.lastIndex(of: viewController) else { return nil }
        let previousIndex = index - 1
        guard previousIndex >= 0 else {return nil}
        guard previousIndex < vcList.count else {return nil}
        return vcList[previousIndex]
        
    }
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let index = vcList.lastIndex(of: viewController) else { return nil }
        let previousIndex = index + 1
        guard previousIndex >= 0 else {return nil}
        guard previousIndex < vcList.count else {return nil}
        return vcList[previousIndex]


    }
}

Output:

iOS PageViewController




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf