Integrate UIStoryboard with PDFViewController in Swift for iOS
Create PDFViewController
in a UIStoryboard.
//// Copyright © 2021-2025 PSPDFKit GmbH. All rights reserved.//// The Nutrient sample applications are licensed with a modified BSD license.// Please see License for details. This notice may not be removed from this file.//
import PSPDFKitimport PSPDFKitUI
#if !os(visionOS)class StoryboardExample: Example { override init() { super.init()
title = "Create PDFViewController in a Storyboard" category = .storyboards priority = 10 }
override func invoke(with delegate: ExampleRunnerDelegate) -> UIViewController? { UIStoryboard(name: "MainStoryboard", bundle: nil).instantiateInitialViewController() }}
// This class should not be private otherwise the Storyboard won’t be able to load it and will fall back to UITableViewController.class StoryboardTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.cellLayoutMarginsFollowReadableWidth = true }
// We don't have enough semantics to tell with just the Storyboard what want to do with the content of the table view cells, so we add some additional logic. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Only apply this if our destination is a PDFViewController. guard let pdfViewController = segue.destination as? PDFViewController, let cell = sender as? UITableViewCell else { return }
// We put files names in the cells so we can use that with the Catalog’s AssetLoader helper. pdfViewController.document = AssetLoader.document(for: AssetName(rawValue: cell.textLabel!.text!)) }}#endif
<?xml version="1.0" encoding="UTF-8"?><document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="NqT-5v-tcw"> <device id="retina6_1" orientation="portrait" appearance="light"/> <dependencies> <deployment identifier="iOS"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> </dependencies> <scenes> <!--Storyboard Example--> <scene sceneID="ziH-O4-qot"> <objects> <tableViewController id="Hud-gb-uQI" customClass="StoryboardTableViewController" customModule="Catalog" customModuleProvider="target" sceneMemberID="viewController"> <tableView key="view" opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" id="edK-4h-PhD"> <rect key="frame" x="0.0" y="0.0" width="414" height="804"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <color key="backgroundColor" red="0.94901960784313721" green="0.94901960784313721" blue="0.96862745098039216" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <sections> <tableViewSection id="ajx-jW-lw2"> <cells> <tableViewCell contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="1dl-3m-0F9" style="IBUITableViewCellStyleDefault" id="A2r-3a-tl3"> <rect key="frame" x="0.0" y="17.5" width="414" height="49.5"/> <autoresizingMask key="autoresizingMask"/> <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="A2r-3a-tl3" id="RVj-D2-cd3"> <rect key="frame" x="0.0" y="0.0" width="383.5" height="49.5"/> <autoresizingMask key="autoresizingMask"/> <subviews> <label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="About PSPDFKit.pdf" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="1dl-3m-0F9"> <rect key="frame" x="20" y="0.0" width="355.5" height="49.5"/> <autoresizingMask key="autoresizingMask"/> <fontDescription key="fontDescription" style="UICTFontTextStyleTitle3"/> <nil key="textColor"/> <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> </label> </subviews> </tableViewCellContentView> <connections> <segue destination="17C-3B-iSi" kind="push" id="Dvw-pC-hTV"/> </connections> </tableViewCell> <tableViewCell contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="rBb-YJ-0iQ" style="IBUITableViewCellStyleDefault" id="mhd-Ww-Vlx"> <rect key="frame" x="0.0" y="67" width="414" height="49.5"/> <autoresizingMask key="autoresizingMask"/> <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="mhd-Ww-Vlx" id="w5m-Pu-LkD"> <rect key="frame" x="0.0" y="0.0" width="383.5" height="49.5"/> <autoresizingMask key="autoresizingMask"/> <subviews> <label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="PSPDFKit for Web.pdf" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="rBb-YJ-0iQ"> <rect key="frame" x="20" y="0.0" width="355.5" height="49.5"/> <autoresizingMask key="autoresizingMask"/> <fontDescription key="fontDescription" style="UICTFontTextStyleTitle3"/> <nil key="textColor"/> <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> </label> </subviews> </tableViewCellContentView> <connections> <segue destination="17C-3B-iSi" kind="push" id="xYX-th-SEQ"/> </connections> </tableViewCell> </cells> </tableViewSection> </sections> </tableView> <extendedEdge key="edgesForExtendedLayout"/> <navigationItem key="navigationItem" title="Storyboard Example" id="Gmw-hg-iGW"> <barButtonItem key="rightBarButtonItem" title="Test Modal" id="gDH-BH-lHu"> <connections> <segue destination="VOw-FZ-T1G" kind="modal" id="2oC-TV-yRn"/> </connections> </barButtonItem> </navigationItem> </tableViewController> <placeholder placeholderIdentifier="IBFirstResponder" id="UDc-ia-w3R" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="517.39130434782612" y="252.45535714285714"/> </scene> <!--View Controller--> <scene sceneID="QfA-B8-l8a"> <objects> <viewController id="17C-3B-iSi" customClass="PSPDFViewController" sceneMemberID="viewController"> <layoutGuides> <viewControllerLayoutGuide type="top" id="IF5-NR-KCc"/> <viewControllerLayoutGuide type="bottom" id="3RH-3l-MRT"/> </layoutGuides> <view key="view" contentMode="scaleToFill" id="iUr-7g-SaG"> <rect key="frame" x="0.0" y="0.0" width="414" height="804"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> </view> <extendedEdge key="edgesForExtendedLayout"/> <navigationItem key="navigationItem" id="iQo-oL-zGS"> <nil key="title"/> <button key="titleView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" id="Fvg-ir-hu7"> <rect key="frame" x="143" y="5.5" width="128" height="33"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/> <state key="normal" title="Button"> <color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> </state> <connections> <segue destination="H0D-4D-jXh" kind="push" id="HcC-PE-ZVq"/> </connections> </button> </navigationItem> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="xUN-Mx-hwl" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="1418.840579710145" y="472.09821428571428"/> </scene> <!--View Controller--> <scene sceneID="RE1-6b-fpn"> <objects> <viewController id="H0D-4D-jXh" sceneMemberID="viewController"> <layoutGuides> <viewControllerLayoutGuide type="top" id="52l-hr-aN1"/> <viewControllerLayoutGuide type="bottom" id="c8Q-Ah-m4x"/> </layoutGuides> <view key="view" contentMode="scaleToFill" id="hsD-Sz-Vp7"> <rect key="frame" x="0.0" y="0.0" width="414" height="804"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> </view> <extendedEdge key="edgesForExtendedLayout"/> <navigationItem key="navigationItem" id="6Mt-4J-E8d"/> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="bcT-o8-GGh" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="2065.217391304348" y="481.47321428571428"/> </scene> <!--View Controller--> <scene sceneID="loU-bj-Q0U"> <objects> <viewController id="skA-jS-ww3" customClass="PSPDFViewController" sceneMemberID="viewController"> <layoutGuides> <viewControllerLayoutGuide type="top" id="zyg-5d-DJt"/> <viewControllerLayoutGuide type="bottom" id="FoQ-Fn-O8j"/> </layoutGuides> <view key="view" contentMode="scaleToFill" id="MDP-Jp-uM5"> <rect key="frame" x="0.0" y="0.0" width="414" height="830"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> </view> <extendedEdge key="edgesForExtendedLayout"/> <navigationItem key="navigationItem" id="KaZ-dj-Amg"/> <userDefinedRuntimeAttributes> <userDefinedRuntimeAttribute type="string" keyPath="document" value="Samples/Nutrient welcome.pdf"/> </userDefinedRuntimeAttributes> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="QjE-FC-FFk" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="2055.072463768116" y="71.651785714285708"/> </scene> <!--Navigation Controller--> <scene sceneID="TVD-RK-WgK"> <objects> <navigationController definesPresentationContext="YES" id="NqT-5v-tcw" sceneMemberID="viewController"> <extendedEdge key="edgesForExtendedLayout"/> <navigationBar key="navigationBar" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" id="si0-Km-ZV8"> <rect key="frame" x="0.0" y="48" width="414" height="44"/> <autoresizingMask key="autoresizingMask"/> </navigationBar> <connections> <segue destination="Hud-gb-uQI" kind="relationship" relationship="rootViewController" id="wgY-wU-QJv"/> </connections> </navigationController> <placeholder placeholderIdentifier="IBFirstResponder" id="S1n-Z3-0ad" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="-114.49275362318842" y="252.45535714285714"/> </scene> <!--Navigation Controller--> <scene sceneID="z3J-va-RIw"> <objects> <navigationController id="VOw-FZ-T1G" sceneMemberID="viewController"> <extendedEdge key="edgesForExtendedLayout"/> <toolbarItems/> <navigationItem key="navigationItem" id="YLk-of-zdD"/> <navigationBar key="navigationBar" contentMode="scaleToFill" id="NDP-vk-Q1v"> <rect key="frame" x="0.0" y="0.0" width="414" height="56"/> <autoresizingMask key="autoresizingMask"/> </navigationBar> <nil name="viewControllers"/> <toolbar key="toolbar" opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="qhA-K5-vZv"> <rect key="frame" x="0.0" y="-10" width="0.0" height="0.0"/> <autoresizingMask key="autoresizingMask"/> </toolbar> <connections> <segue destination="skA-jS-ww3" kind="relationship" relationship="rootViewController" id="RzL-oo-uTB"/> </connections> </navigationController> <placeholder placeholderIdentifier="IBFirstResponder" id="oyS-Pi-0cB" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="1418.840579710145" y="71.651785714285708"/> </scene> </scenes> <inferredMetricsTieBreakers> <segue reference="xYX-th-SEQ"/> </inferredMetricsTieBreakers></document>
This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.