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.
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:
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.addChild(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(toParent: containerController)
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.
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.
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.
Make sure your container controller implements:
func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize
- (CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize;
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
- (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.
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
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
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
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.
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.