Customize a document view controller on iOS

Legacy Viewer

This guide is for the legacy PTDocumentViewController class. This class is no longer being updated. For the best viewing experience and to take advantage of new developments, the PTDocumentController should be used. Please see this guide for information.

This article explains how to customize the document viewer classes PTDocumentViewController and PTTabbedDocumentViewController.

Because the document viewer classes are part of the open source Tools UI framework, it is possible to achieve virtually any required modification. That said, it is usually faster and more convenient to configure the viewers via APIs, which this guide describes.

PTDocumentViewController

Apryse Docs Image

The image on the left indicates areas that are controllable via the PTDocumentViewController's API. Information on customizing these is available directly below.

The image on the right indicates a number of default buttons that create and present new controls. Information on where to look to customize these presented controls can be found in the component controls table.

1: Left bar button item

When presented in a UINavigationController, the PTDocumentViewController's left bar button item will display the name of the previous view controller's navigation item's title (or, if set, its backBarButtonItem). When presented as a UINavigationController's root controller, the space will be empty.

The PTDocumentViewController's left bar button item can be added or replaced as follows:

1let title = NSLocalizedString("Documents", comment: "Go back to the document picker.")
2documentViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "title, style: .plain, target: self, action: #selector(self.pressedLeft(_:)))"
3...
4
5@objc func pressedLeft(_ item: UIBarButtonItem)
6{
7 // handle bar button item action
8}

2: Right bar button items

These buttons, which are contained in the rightBarButtonItems array, are completely customizable. It is possible to

Remove buttons

Default buttons can be removed ("hidden") using built-in properties. From left-to-right:

For example to hide the share and viewer settings buttons:

1documentViewController.shareButtonHidden = true
2documentViewController.viewerSettingsButtonHidden = true

Add buttons

Buttons can be added by accessing the appropriate BarButtonItem array and adding a button:

Adds a button to the top navigation bar:

1// add a button to the top bar
2let plusButtonTop = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.writeButtonName(_:)))
3
4if let rightItems = documentViewController.navigationItem.rightBarButtonItems {
5 documentViewController.navigationItem.rightBarButtonItems = rightItems + [plusButtonTop]
6}
7
8// add a button to the right of the page slider
9let refreshButtonBottom = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(self.writeButtonName(_:)))
10
11if let bottomItems = documentViewController.thumbnailSliderController.trailingToolbarItems{
12 documentViewController.thumbnailSliderController.trailingToolbarItems = bottomItems + [refreshButtonBottom]
13}

Move buttons

The default buttons are all accessible via properties, making it easy to rearrange or move them. The following code swaps the position of the search button and navigation lists button:

1var rightItems = documentViewController.navigationItem.rightBarButtonItems
2rightItems?.removeAll(where: { element in element == documentViewController.searchButtonItem })
3rightItems?.append(documentViewController.navigationListsButtonItem)
4
5var bottomRightItems:[UIBarButtonItem]? = documentViewController.thumbnailSliderController.trailingToolbarItems
6bottomRightItems?.removeAll(where: { element in element as NSObject == documentViewController.navigationListsButtonItem })
7bottomRightItems?.append(documentViewController.searchButtonItem)
8
9documentViewController.navigationItem.rightBarButtonItems = rightItems
10documentViewController.thumbnailSliderController.trailingToolbarItems = bottomRightItems

Change icons

The icons of existing buttons may be changed by creating new UIBarButtonItems that have the same target and action as an existing item, and replacing the existing item with the new item:

1// new share UIBarButtonItem
2let newShareItem = UIBarButtonItem(barButtonSystemItem: .action, target: documentViewController.shareButtonItem.target, action: documentViewController.shareButtonItem.action)
3
4// replace old share UIBarButtonItem with new share UIBarButtonItem
5var rightItems = documentViewController.navigationItem.rightBarButtonItems
6var index: Int? = (rightItems as NSArray?)?.index(of: documentViewController.shareButtonItem)
7rightItems?.removeAll(where: { element in element == documentViewController.shareButtonItem })
8rightItems?.insert(newShareItem, at: index ?? 0)
9
10// new search UIBarButtonItem
11let newSearchItem = UIBarButtonItem(barButtonSystemItem: .search, target: documentViewController.searchButtonItem.target, action: documentViewController.searchButtonItem.action)
12
13// replace old search UIBarButtonItem with new search UIBarButtonItem
14index = (rightItems as NSArray?)?.index(of: documentViewController.searchButtonItem)
15rightItems?.removeAll(where: { element in element == documentViewController.searchButtonItem })
16rightItems?.insert(newSearchItem, at: index ?? 0)
17
18// update the icons
19documentViewController.navigationItem.rightBarButtonItems = rightItems

3: PDFViewCtrl

The PTPDFViewCtrl is a UIView that displays the PDF. It is customizable via is properties/methods and delegate methods.

For an overview see the PTPDFViewCtrl Guide , or the detailed API documentation.

Note that all PDF "interaction" (annotations, form filling, text selection, link following, etc.) is supplementary to the PDFViewCtrl, and is implemented in the open source tools.framework.

4: Page number indicator

The page indicator can be enabled/disabled via the pageIndicatorEnabled property.

5: Thumbnail slider controller

The thumbnail slider can be enabled/disabled via the bottomToolbarEnabled property.

The default buttons presented to the right and left of the slider are easily hidden through the convenience properties thumbnailBrowserButtonHidden and navigationListsButtonHidden on the PTDocumentViewController:

1documentViewController.thumbnailBrowserButtonHidden = true

6-12: Controls Presented by a PTDocumentViewController

To customize the controls that are presented by the PTDocumentViewController's default buttons, please see the corresponding guide or API:

PTTabbedDocumentViewController

The tabbed document view controller displays a collection of document viewer controllers in tabs.

Tab Settings

Tabs can be disabled using the tabsEnabled property, and the maximum number of allowed tabs can be set using maximumTabCount.

Access to child PTDocumentViewControllers

The current document view controller can be accessed via selectedViewController, and others via documentViewController(at:)

To configure a document view controller before it is displayed, conform to and implement the PTTabbedDocumentViewControllerDelegate method tabbedDocumentViewController(_:willAdd:). Note that it is permissible to assign the internal PTDocumentViewController's delegate to an external object.

1func tabbedDocumentViewController(_ tabbedDocumentViewController: PTTabbedDocumentViewController, willAdd documentViewController: PTDocumentViewController) {
2 documentViewController.delegate = self
3 // customize documentViewController
4 }

Summary

API

Functionality

tabsEnabled

Enables/disables tabs.

maximumTabCount

Controls the maximum number of concurrent tabs.

selectedViewController

The current PTDocumentViewController.

documentViewController(at:)

The PTDocumentViewController at the given index.

tabbedDocumentViewController(_:willAdd:)

Access to a PTDocumentViewController that is about to be displayed.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales