swift – iOS PDFKit’s PDFView not scrollable


Making an attempt to create a easy PDF viewer utilizing PDFKit. The PDF file is seen as anticipated however it isn’t scrollable vertically. The PDFView is created programmatically inside one other UIView subclass as proven beneath:

import UIKit
import PDFKit

class MyPDFViewerView: UIView {
    
    personal var pdfFileURL: String?
    
    // MARK: - UI Elements
    
    personal let pdfView: PDFView
    
    // MARK: - Init
    
    init(fileURL: String) {
        self.pdfFileURL = fileURL
        pdfView = PDFView(body: .zero)
        tremendous.init(body: .zero)
        createComponents()
        addConstraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) not but implementd")
    }
    
    // MARK: - Setup UI
    
    personal func createComponents() {
        [pdfView].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            addSubview($0)
        }
        pdfView.isUserInteractionEnabled = true
        pdfView.autoScales = true
        pdfView.displayDirection = .vertical
        pdfView.pageBreakMargins = .zero
    }
    
    personal func addConstraints() {
        NSLayoutConstraint.activate([
            pdfView.leadingAnchor.constraint(equalTo: leadingAnchor),
            pdfView.trailingAnchor.constraint(equalTo: trailingAnchor),
            pdfView.topAnchor.constraint(equalTo: topAnchor),
            pdfView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }
    
    // MARK: - Helpers
    
    func loadPDFDocument() {
        guard let fileURLString = pdfFileURL else {
            return
        }
        if !FileManager.default.fileExists(atPath: fileURLString) {
            return
        }
        let fileURL = URL(fileURLWithPath: fileURLString)
        if let doc = PDFDocument(url: fileURL) {
            pdfView.doc = doc
        }
    }
}

The category MyPDFViewerView is added as a subview in a UIViewController’s view. What am I doing improper right here ?

Tried delaying the insertion of view MyPDFViewerView as a subview of ViewController’s view however nonetheless no luck.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles