C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PageViewControllerIn 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
UIPageViewController Functions
ExampleIn 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. 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. 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:
Next TopicSplit View Controller
|