Implementing a Page based iOS 7 Application using UIPageViewController

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
An iPad iOS 7 Split View and Popover ExampleAn Example iOS 7 UIPageViewController Application


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


The UIPageViewController class was introduced into the iOS 5 SDK as a mechanism to implement a page turning style of user interface in iOS applications. This chapter will provide a brief overview of the concepts behind the page view controller before working through the development of an example application in the next chapter.


Contents


The UIPageViewController Class

The UIPageViewController class highlights the distinction between a view controller and a container controller. A view controller is responsible for managing a user interface view, typically in the form of a view hierarchy contained within a storyboard or Interface Builder NIB file. Creating a Single View Application with Xcode, for example, will create a single view controller class with a corresponding entry in the storyboard file.

A container controller, on the other hand, is a class designed to contain and manage multiple view controllers, usually providing a mechanism for switching from one view controller to another, either programmatically or in response to user interaction.

The UIPageViewController class is a container controller designed to facilitate navigation through multiple views using a page curling visual transition. When implemented, this allows users to page through content using screen based gestures, much in the same way Apple’s iBooks application allows a user to move backwards and forwards within the pages of an eBook using page turning gestures. Each page displayed to the user is actually a view controller created on demand for the page controller by a data source.

The UIPageViewController DataSource

In order to function, a UIPageViewController instance must be assigned a data source which, in turn, is responsible for providing view controller objects as required for each page. The data source takes the form of a class instance that implements the <UIPageViewControllerDataSource> protocol which, at a minimum, must implement the following two methods:

  • viewControllerAfterViewController: - This method is passed a view controller representing the currently displayed page and is required to return the view controller corresponding to the next page in the paging sequence.
  • viewControllerBeforeViewController: - This method is passed the view controller representing the currently displayed page and is required to return the view controller corresponding to the previous page in the paging sequence.

The mechanism used to create the requested view controllers and the content therein will generally be application specific and is at the discretion of the developer. Apple does, however, recommend that in order to ensure optimal performance and minimal resource usage, the view controllers be created on an as-needed basis rather than pre-created.

When a UIPageViewController object is initialized, a number of configuration options may be specified to configure the appearance and behavior of the contained views.

Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


Navigation Orientation

The page controller is capable of transitioning between views using either a vertical or horizontal paradigm. In the case of horizontal navigation, page transitions take place in the same way pages are turned in a physical book by sweeping a finger on the screen either left or right.

In the case of horizontal navigation, pages are turned by making vertical gestures in much the same way the pages of a wall calendar are flipped. These options are configured using the following constants:

  • UIPageViewControllerNavigationOrientationHorizontal
  • UIPageViewControllerNavigationOrientationVertical

Spine Location

The UIPageViewController class allows for the location of the spine to be configured. The term spine in this context is analogous to the spine of a book and dictates the location of the axis on which each page will turn.

The behavior of the spine location settings vary depending on the navigation orientation setting. For example, the default for most configurations is UIPageViewControllerSpineLocationMin which places the spine on the left hand side or top of the screen depending on whether the navigation orientation is horizontal or vertical. Similarly, the UIPageViewControllerSpineLocationMax setting will position the spine at the right or bottom edge of the display. In order to display two pages simultaneously the UIPageViewControllerSpineLocationMid setting should be used.

The view controller may also be configured to treat pages as being double sided via the doubleSided property.

Note that when using UIPageViewControllerSpineLocationMid spine location it will be necessary to provide the page controller with two view controllers (one for the left hand page and one for the right) for each page turn. Similarly, when using either the min or max spine location together with the double sided setting, view controllers for both the front and back of the current page will be required for each page.

The UIPageViewController Delegate Protocol

In addition to a data source, instances of the UIPageViewController class may also be assigned a delegate which, in turn, may implement the following delegate methods:

  • spineLocationForInterface: - The purpose of this delegate method is to allow the spine location to be changed in the event that the device is rotated by the user. An application might, for example, switch to UIPageViewControllerSpineLocationMid layout when the device is placed in a landscape orientation. The method is passed the new orientation and must return a corresponding spine location value. Before doing so it may, for example, also set up two view controllers if a switch is being made to a UIPageViewControllerSpineLocationMid spine location.
  • transitionComplete: - This method is called after the user initiates a page transition via a screen based gesture. The success or otherwise of the transition may be identified through the implementation of a completion handler.

Summary

The UIPageViewController class is categorized as a container controller in that it is responsible for containing view controllers and managing which view is displayed to the user. The main purpose of the UIPageViewController is to allow the user to navigate through different views using a page curl transition style. Implementation of this functionality requires the configuration of navigation orientation, spine location and a number of data source methods.

The theory covered in this chapter will be put into practice in the next chapter entitled An Example iOS 7 UIPageViewController Application.


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of ContentsNext
An iPad iOS 7 Split View and Popover ExampleAn Example iOS 7 UIPageViewController Application