Embedding the PSPDFViewController Inside a Custom Container View Controller


PSPDFKit is designed to work well with UIKit's container view controller API so that you can easily embed a PSPDFViewController inside your custom container view controller implementation. The following steps will guide you trough the setup.

Add the PSPDFViewController as a child view controller

First you need to make sure the PSPDFViewController is properly added to your container view controller. A good read about how to do this is Apple's Implementing a Custom Container View Controller. In our case we want to add a pdf view controller fullscreen inside the container view controller. So our code to do that looks like this:

Copy
1
2
3
4
5
6
7
8
let containerController = ... // this is your custom container view controller.
let pdfController = ... // this is the configured instance of `PSPDFViewController` that you want to add.

containerController.addChildViewController(pdfController)
pdfController.view.frame = containerController.view.bounds // make the pdfController fullscreen in your container controller
pdfController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] // ensure the pdfController resizes along with your container controller
containerController.view.addSubview(pdfController.view)
pdfController.didMove(toParentViewController: containerController)
Copy
1
2
3
4
5
6
7
8
UIViewController *containerController = ...; // this is your custom container view controller.
PSPDFViewController *pdfController = ...; // this is the configured instance of `PSPDFViewController` that you want to add.

[containerController addChildViewController:pdfController];
pdfController.view.frame = containerController.view.bounds; // make the pdfController fullscreen in your container controller
pdfController.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); // ensure the pdfController resizes along with your container controller
[containerController.view addSubview:pdfController.view];
[pdfController didMoveToParentViewController:containerController];

For the most parts this is listing 5-1 from the above mentioned guide but we also make sure that the pdf controller resizes properly whenever the container view controller's view changes its bounds. If you are using auto layout inside your container view controller, you may need to alter the code to position and resize the pdf controller correctly.

Non-fullscreen presentation

Sometimes the size of embedded PSPDFViewController doesn't match the size of the container controller. It is usually when the container controller adds some decorations around PSPDFViewController (e.g. margins, custom navigation bars or toolbars).

In such case you need to be aware that it's the container's responsibility to correctly define the size of its child controllers. Here are requirements you need to follow.

Initialization

Make sure the correct frame is set on PSPDFViewController's view before you add it to your view hierarchy. Changing the size of PSPDFViewController's view during appearance transition might cause UI issues.

If your container has complex layout it might be easier to add PSPDFViewController after the layout pass when you already know the correct frame for PSPDFViewController's view.

Size transitions

Make sure your container controller implements:

Copy
1
func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize
Copy
1
- (CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize;

and returns to correct size for PSPDFViewController. This method is called during size transitions (including rotations) and the result is then passed to PSPDFViewController in:

Copy
1
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
Copy
1
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator;

PSPDFViewController uses this size to properly configure itself. Neglecting this might cause layout glitches and break automatic page mode.

Custom container view controllers and UINavigationController

If you integrate the PSPDFViewController inside your custom view controller you can still add your custom view controller inside a navigation controller afterwards. However there are a couple of things to keep in mind.

The PSPDFViewController will no longer take care of the navigation bar, as the immediate child of the navigation controller is responsible for doing this. You can set useParentNavigationBar to YES in the PSPDFConfiguration and we will do our best to update the navigation bar for you, however this may not work well in all edge cases and we may require your help.

If you configure the PSPDFViewController so that it should hide the navigation bar when the user taps on the screen (-[PSPDFConfiguration shouldHideNavigationBarWithUserInterface] and -[PSPDFConfiguration userInterfaceViewMode]) and you are using opaque navigation bars (i.e. set -[UINavigationBar translucent] to NO), make sure to also set extendedLayoutIncludesOpaqueBars to YES on your container view controller as well. Otherwise the view of your container view controller will be resized every time the navigation bar changes its visibility state. This also means that the PSPDFViewController will get resized in the same way, resulting in an up/down jump of the pages that are currently visible.

In general, when adding a custom container view controller between any kind of UIKit container view controllers (such as UINavigationController, UITabBarController, or UISplitViewController) make sure that the view controller hierarchy is setup correctly. The PSPDFViewController's parent view controller should always be your container view controller in that case. You should not set the pdf view controller to become a child of the UIKit controller but then embed it into your custom container view controller's view hierarchy.

Also keep in mind that everything we normally do when interacting with the default UIKit container view controllers, we no longer do. From the perspective of the PSPDFViewController your custom container view controller now is the parent we adopt to. It is your custom container view controller that is now responsible for the communication and proper handling of its parent UIKit container view controller.

Samples

If you want to look through more code, there are a couple of samples available in our catalog project. Just have a look at the ViewController Customization section.

Was this page helpful? We're happy to answer any questions.