Customize a document view controller on iOS

This document refers to the PTDocumentController

For the legacy PTDocumentViewController class, please see this guide .

This article explains how to customize the document viewer classes PTDocumentController 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.

PTDocumentController

Apryse Docs Image

The above image (items 1–5) indicates areas that are controllable via the PTDocumentController's API. Information on customizing these is available directly below.

Apryse Docs Image

The above image (items 7–12) 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.

On iPad and other regular horizontal size class devices, a number of default UI controls are accessible from the navigation bar, whereas on iPhones and compact size classes some of those controls appear in the toolbar at the bottom of the view.

Image Number

Button

Functionality

Location on iPad

Location on iPhone

7

searchButtonItem

PTTextSearchViewController

navigationItem.rightBarButtonItems

navigationItem.rightBarButtonItems

8

moreItemsButtonItem

PTMoreItemsViewController

navigationItem.rightBarButtonItems

navigationItem.rightBarButtonItems

10

navigationListsButtonItem

PTNavigationListsViewController

navigationItem.leftBarButtonItems

toolbar

11

thumbnailsButtonItem

PTThumbnailsViewController

navigationItem.leftBarButtonItems

toolbar

12

readerModeButtonItem

PTReflowViewController

navigationItem.rightBarButtonItems

toolbar

1: Toolbar switcher

Hide the toolbar switcher

The toolbar switcher can be hidden:

1documentController.toolGroupIndicatorView.isHidden = true

Remove toolbars from the switcher

Toolbars can be removed by removing them from the toolGroupManager's groups array. The following code removes the "Draw" and "Pens" toolbars:

1var mutableGroups = documentController.toolGroupManager.groups
2
3let groupsToRemove = [documentController.toolGroupManager.drawItemGroup, documentController.toolGroupManager.pensItemGroup]
4
5mutableGroups.removeAll(where: {groupsToRemove.contains($0)})
6
7documentController.toolGroupManager.groups = mutableGroups

2: Navigation bar items

The buttons which are contained in the leftBarButtonItems and rightBarButtonItems arrays, are completely customizable. It is possible to

Add buttons

You can modify the navigation bar items. Here is an example of adding a new button (for the current size class):

1let myItem = UIBarButtonItem(image: UIImage(systemName:"square.and.pencil"), style: .plain, target: nil, action: nil)
2
3documentController.navigationItem.rightBarButtonItems.append(myItem)

Tools can also be added:

1let freeHand = documentController.toolGroupManager.createItem(forToolClass:PTFreeHandCreate.self)
2
3documentController.navigationItem.rightBarButtonItems.append(freeHand)

Note that in the example above, de-selecting the tool button item needs to be implemented by the app, by listening to the Tool Did Change notification.

Remove buttons

To remove any of the default buttons see the instructions here: Hide default buttons

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 search UIBarButtonItem
2let newSearchItem = UIBarButtonItem(barButtonSystemItem: .search, target: documentController.searchButtonItem.target, action: documentController.searchButtonItem.action)
3
4// replace old search UIBarButtonItem with new search UIBarButtonItem
5index = (rightItems as NSArray?)?.index(of: documentController.searchButtonItem)
6rightItems?.removeAll(where: { element in element == documentController.searchButtonItem })
7rightItems?.insert(newSearchItem, at: index ?? 0)
8
9// update the icons
10documentController.navigationItem.rightBarButtonItems = rightItems

3: Annotation toolbar

The currently-visible annotation toolbar is selected with the toolbar switcher.

Hide (and show) the annotation toolbar

The toolbar can be programmatically hidden by setting the mode to view group, which is a special group and the only group where the toolbar is hidden:

1documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.viewItemGroup

The toolbar can be shown again by changing the group to any group other than view:

1documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.drawItemGroup

Remove buttons from a toolbar

The example below shows how to remove the text highlight and text underline button from a toolbar.

Disabling a tool type entirely

If you want to disable a tool entirely, from all toolbars and the long press menu, please use the annotations permissions system.

1let annotateGroup = documentController.toolGroupManager.annotateItemGroup
2
3// tool buttons that exist currently
4let defaultAnnotateGroupTools = annotateGroup.barButtonItems
5
6// new set of tools to replace current ones
7var newAnnotateGroupTools = [UIBarButtonItem]()
8
9// add all currently existing tools except for the ones we don't want
10for defaultToolItem in defaultAnnotateGroupTools
11{
12 if defaultToolItem.isKind(of: PTToolBarButtonItem.self) {
13 let toolBarButton = defaultToolItem as! PTToolBarButtonItem
14 if toolBarButton.toolClass == PTTextHighlightCreate.self ||
15 toolBarButton.toolClass == PTTextUnderlineCreate.self
16 {
17 // do not add this tool
18 continue
19 }
20 else
21 {
22 newAnnotateGroupTools.append(toolBarButton)
23 }
24 }
25 else
26 {
27 newAnnotateGroupTools.append(defaultToolItem)
28 }
29}
30
31// assign tools to new array
32documentController.toolGroupManager.annotateItemGroup.barButtonItems = newAnnotateGroupTools

Add a tool button to a toolbar

1// create a mutable array of the current items in the annotation toolbar group
2var availableTools:[UIBarButtonItem] = documentController.toolGroupManager.annotateItemGroup.barButtonItems
3
4// create a new toolbar item for freehand annotations
5let freeHandItem = documentController.toolGroupManager.createItem(forToolClass: PTFreeHandCreate.self)
6
7// add the freehand annotation item to the front of the list
8availableTools.insert(freeHandItem, at: 0)
9
10// assign the array back to the annotation toolbar group.
11documentController.toolGroupManager.annotateItemGroup.barButtonItems = availableTools

Add a button with fully custom behavior

Your app may need a button that does not invoke one of the built in annotation tools. The following code will add a button that calls a selector.

1let selectableItem = PTSelectableBarButtonItem(image: UIImage(systemName:"square.and.pencil"), style: .plain, target: self, action: #selector(customToolAction(_:)))
2selectableItem.title = "Custom Tool"
3
4documentController.toolGroupManager.annotateItemGroup.barButtonItems.append(selectableItem)

If you want to toggle the items selection, flip its selected property:

1@objc func customToolAction(_ button:PTSelectableBarButtonItem)
2{
3 button.isSelected = !button.isSelected
4}

4: Page number indicator

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

5: 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.

6. Toolbar (iPhone only)

These are the buttons that appear at the bottom of the screen on iPhones and in Compact horizontal size classes. Buttons can be added, removed, or rearranged with convenient APIs.

Add buttons to toolbar

1// new button
2let myItem = UIBarButtonItem(image: UIImage(systemName:"square.and.pencil"), style: .plain, target: nil, action: nil)
3
4// spacer to keep evenly spaced buttons
5let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
6
7// new array
8var toolbarItems = documentController.toolbarItems
9
10// add the new items
11toolbarItems.append(contentsOf: [spacer, myItem])
12
13// set the toolbarItems to the new items
14documentController.toolbarItems = toolbarItems

Remove buttons from toolbar

To remove any of the default buttons see the instructions here: Hide default buttons

7-12: Controls Presented by a PTDocumentController

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

Hide default buttons

Default buttons can be removed ("hidden") using built-in properties.

For example to hide the search and more items buttons:

1documentController.searchButtonHidden = true
2documentController.moreItemsButtonHidden = true

PTTabbedDocumentViewController

The tabbed document view controller displays a collection of document 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 PTDocumentControllers

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

To configure a document 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