Configuring Scroll Directions and Page Transitions in Our Viewer

You can configure the page transition, scroll direction, and scroll mode of a PDFViewController in PDFConfiguration (see the PDF configuration guide for more information).

In SettingsExample from the Catalog app, you’ll see a complete example of how these properties interact together in the context of a PDFSettingsViewController(opens in a new tab). A PDFSettingsViewController(opens in a new tab) is used to change key UX settings that are configurable via the PDFConfiguration.settingsOptions(opens in a new tab) property.

For the best user experience, we recommend using one of the following two combinations of settings.

Book/Magazine-Style Scrolling

let configuration = PDFConfiguration { builder in
builder.scrollDirection = .horizontal
builder.pageTransition = .scrollPerSpread
builder.pageMode = .automatic
builder.spreadFitting = .fit
}

This will show two pages at a time when there is sufficient space (typically on iPad in landscape), and one page at a time in all other situations.

Continuous Vertical Scrolling

let configuration = PDFConfiguration { builder in
builder.scrollDirection = .vertical
builder.pageTransition = .scrollContinuous
builder.pageMode = .single
builder.spreadFitting = .adaptive
}

This will show pages fit to the window size in most cases or filling the width on iPhone in landscape.

Valid Combinations

You need to be aware of the following constraints when using the PDFConfiguration properties (pageTransition(opens in a new tab), scrollDirection(opens in a new tab), and pageMode(opens in a new tab)) simultaneously.

Per-Spread Scrolling

If the page transition is PageTransition.scrollPerSpread(opens in a new tab), there are no constraints:

let configuration = PDFConfiguration { builder in
// When the page transition is `scrollPerPage`.
builder.pageTransition = .scrollPerSpread
builder.scrollDirection = .vertical // Can also be `.horizontal`.
builder.pageMode = .single // Can also be `.double` or `.automatic`.
}

Continuous Scrolling

If the page transition is PageTransition.scrollContinuous(opens in a new tab):

let configuration = PDFConfiguration { builder in
// When the page transition is `scrollContinuous`.
builder.pageTransition = .scrollContinuous
builder.scrollDirection = .vertical // Can also be `.horizontal`.
builder.pageMode = .single // Setting a value to `pageMode` will only be honored if `scrollDirection` is `.vertical`. It will be forced to single otherwise.
}

Page Curl

If the page transition is PageTransition.curl(opens in a new tab):

let configuration = PDFConfiguration { builder in
// When the page transition is `curl`.
builder.pageTransition = .curl
builder.scrollDirection = .horizontal // Setting a value to the `scrollDirection` property will be ignored. It will be forced to `.horizontal`.
builder.pageMode = .single // Can also be `.double` or `.automatic`.
}

Persisting the Settings Options with User Defaults

You can persist the settings options for your PDFSettingsViewController(opens in a new tab) in your app’s UserDefaults(opens in a new tab).

Saving the Settings Options

You can save the settings in your app’s user defaults in PDFViewControllerDelegate.pdfViewControllerDidDismiss(_:)(opens in a new tab), like so:

func pdfViewControllerDidDismiss(_ pdfController: PDFViewController) {
// Persist the settings options in the user defaults.
let defaults = UserDefaults.standard
defaults.set(pdfController.configuration.pageTransition.rawValue, forKey: "pageTransition")
defaults.set(pdfController.configuration.pageMode.rawValue, forKey: "pageMode")
defaults.set(pdfController.configuration.scrollDirection.rawValue, forKey: "scrollDirection")
defaults.set(pdfController.configuration.spreadFitting.rawValue, forKey: "spreadFitting")
}

Restoring the Settings Options

You can restore the saved settings options from your app’s user defaults in your PDFConfiguration, as seen below:

let configuration = PDFConfiguration { builder in
// Restore the settings from the user defaults.
let defaults = UserDefaults.standard
builder.pageTransition = PageTransition(rawValue: UInt(defaults.integer(forKey: "pageTransition")))!
builder.pageMode = PageMode(rawValue: UInt(defaults.integer(forKey: "pageMode")))!
builder.scrollDirection = ScrollDirection(rawValue: UInt(defaults.integer(forKey: "scrollDirection")))!
builder.spreadFitting = PDFConfiguration.SpreadFitting(rawValue: defaults.integer(forKey: "spreadFitting"))!
}

For more details and sample code, take a look at PersistViewSettingsExample.swift(opens in a new tab) from the Catalog app.