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.
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 .
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:
Swift Obj-C
1 let title = NSLocalizedString ( " Documents " , comment : " Go back to the document picker. " )
2 documentViewController.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 }
1 // e.g. in viewDidLoad
2 NSString * title = NSLocalizedString ( @" Documents " , @" Go back to the document picker. " );
3 self .myDocumentViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc ] initWithTitle :title style :UIBarButtonItemStylePlain target : self action : @ selector ( action: )];
4 ...
5
6 - ( void )action:(UIBarButtonItem * )barButtonItem
7 {
8 // handle bar button item action
9 }
These buttons, which are contained in the rightBarButtonItems
array, are completely customizable. It is possible to
Default buttons can be removed ("hidden") using built-in properties. From left-to-right:
For example to hide the share and viewer settings buttons:
Swift Obj-C
1 documentViewController.shareButtonHidden = true
2 documentViewController.viewerSettingsButtonHidden = true
1 documentViewController.shareButtonHidden = YES ;
2 documentViewController.viewerSettingsButtonHidden = YES ;
Buttons can be added by accessing the appropriate BarButtonItem array and adding a button:
Adds a button to the top navigation bar:
Swift Obj-C
1 // add a button to the top bar
2 let plusButtonTop = UIBarButtonItem ( barButtonSystemItem : .add, target : self , action : #selector ( self . writeButtonName (_:)))
3
4 if let rightItems = documentViewController.navigationItem.rightBarButtonItems {
5 documentViewController.navigationItem.rightBarButtonItems = rightItems + [plusButtonTop]
6 }
7
8 // add a button to the right of the page slider
9 let refreshButtonBottom = UIBarButtonItem ( barButtonSystemItem : .refresh, target : self , action : #selector ( self . writeButtonName (_:)))
10
11 if let bottomItems = documentViewController.thumbnailSliderController.trailingToolbarItems{
12 documentViewController.thumbnailSliderController.trailingToolbarItems = bottomItems + [refreshButtonBottom]
13 }
1 // add a button to the top bar
2 UIBarButtonItem * plusButtonTop = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem :UIBarButtonSystemItemAdd target : self action : @ selector ( writeButtonName: )];
3
4 documentViewController.navigationItem.rightBarButtonItems = [documentViewController.navigationItem.rightBarButtonItems arrayByAddingObject :plusButtonTop];
5
6 // add a button to the right of the page slider
7 UIBarButtonItem * refreshButtonBottom = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem :UIBarButtonSystemItemRefresh target : self action : @ selector ( writeButtonName: )];
8
9 documentViewController.thumbnailSliderController.trailingToolbarItems = [documentViewController.thumbnailSliderController.trailingToolbarItems arrayByAddingObject :refreshButtonBottom];
10
11 - ( void )writeButtonName:(UIBarButtonItem * )barButtonItem
12 {
13 NSLog ( @" %@ was pressed. " , barButtonItem);
14 }
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:
Swift Obj-C
1 var rightItems = documentViewController.navigationItem.rightBarButtonItems
2 rightItems ? . removeAll ( where : { element in element == documentViewController.searchButtonItem })
3 rightItems ? . append (documentViewController.navigationListsButtonItem)
4
5 var bottomRightItems:[UIBarButtonItem] ? = documentViewController.thumbnailSliderController.trailingToolbarItems
6 bottomRightItems ? . removeAll ( where : { element in element as NSObject == documentViewController.navigationListsButtonItem })
7 bottomRightItems ? . append (documentViewController.searchButtonItem)
8
9 documentViewController.navigationItem.rightBarButtonItems = rightItems
10 documentViewController.thumbnailSliderController.trailingToolbarItems = bottomRightItems
1 NSMutableArray * rightItems = [documentViewController.navigationItem.rightBarButtonItems mutableCopy ];
2 [rightItems removeObject :documentViewController.searchButtonItem];
3 [rightItems addObject :documentViewController.navigationListsButtonItem];
4
5 NSMutableArray * bottomRightItems = [documentViewController.thumbnailSliderController.trailingToolbarItems mutableCopy ];
6 [bottomRightItems removeObject :documentViewController.navigationListsButtonItem];
7 [bottomRightItems addObject :documentViewController.searchButtonItem];
8
9 documentViewController.navigationItem.rightBarButtonItems = [rightItems copy ];
10 documentViewController.thumbnailSliderController.trailingToolbarItems = [bottomRightItems copy ];
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:
Swift Obj-C
1 // new share UIBarButtonItem
2 let newShareItem = UIBarButtonItem ( barButtonSystemItem : .action, target : documentViewController.shareButtonItem.target, action : documentViewController.shareButtonItem.action)
3
4 // replace old share UIBarButtonItem with new share UIBarButtonItem
5 var rightItems = documentViewController.navigationItem.rightBarButtonItems
6 var index: Int ? = (rightItems as NSArray ? ) ? . index ( of : documentViewController.shareButtonItem)
7 rightItems ? . removeAll ( where : { element in element == documentViewController.shareButtonItem })
8 rightItems ? . insert (newShareItem, at : index ?? 0 )
9
10 // new search UIBarButtonItem
11 let newSearchItem = UIBarButtonItem ( barButtonSystemItem : .search, target : documentViewController.searchButtonItem.target, action : documentViewController.searchButtonItem.action)
12
13 // replace old search UIBarButtonItem with new search UIBarButtonItem
14 index = (rightItems as NSArray ? ) ? . index ( of : documentViewController.searchButtonItem)
15 rightItems ? . removeAll ( where : { element in element == documentViewController.searchButtonItem })
16 rightItems ? . insert (newSearchItem, at : index ?? 0 )
17
18 // update the icons
19 documentViewController.navigationItem.rightBarButtonItems = rightItems
1 // new share UIBarButtonItem
2 UIBarButtonItem * newShareItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem :UIBarButtonSystemItemAction target :documentViewController.shareButtonItem.target action :documentViewController.shareButtonItem.action];
3
4 // replace old share UIBarButtonItem with new share UIBarButtonItem
5 NSMutableArray * rightItems = [documentViewController.navigationItem.rightBarButtonItems mutableCopy ];
6 NSUInteger index = [rightItems indexOfObject :documentViewController.shareButtonItem];
7 [rightItems removeObject :documentViewController.shareButtonItem];
8 [rightItems insertObject :newShareItem atIndex :index];
9
10 // new search UIBarButtonItem
11 UIBarButtonItem * newSearchItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem :UIBarButtonSystemItemSearch target :documentViewController.searchButtonItem.target action :documentViewController.searchButtonItem.action];
12
13 // replace old search UIBarButtonItem with new search UIBarButtonItem
14 index = [rightItems indexOfObject :documentViewController.searchButtonItem];
15 [rightItems removeObject :documentViewController.searchButtonItem];
16 [rightItems insertObject :newSearchItem atIndex :index];
17
18 // update the icons
19 documentViewController.navigationItem.rightBarButtonItems = [rightItems copy ];
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
.
The page indicator can be enabled/disabled via the pageIndicatorEnabled
property.
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
:
Swift Obj-C
1 documentViewController.thumbnailBrowserButtonHidden = true
1 documentViewController.thumbnailBrowserButtonHidden = YES ;
To customize the controls that are presented by the PTDocumentViewController
's default buttons, please see the corresponding guide or API:
The tabbed document view controller displays a collection of document viewer controllers in tabs.
Tabs can be disabled using the tabsEnabled
property, and the maximum number of allowed tabs can be set using maximumTabCount
.
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.
Swift Obj-C
1 func tabbedDocumentViewController ( _ tabbedDocumentViewController : PTTabbedDocumentViewController, willAdd documentViewController : PTDocumentViewController) {
2 documentViewController.delegate = self
3 // customize documentViewController
4 }
1 - ( void )tabbedDocumentViewController:(PTTabbedDocumentViewController * )tabbedDocumentViewController willAddDocumentViewController:(PTDocumentViewController * )documentViewController
2 {
3 documentViewController.delegate = self ;
4 // customize documentViewController
5 }