Jeremie
Jeremie

Reputation: 2411

How to create a single page vertical scrolling PDFView in Swift

I am trying to make a vertical scrolling PDFView using the following code:

pdfView = PDFView(frame: view.frame)
pdfView.backgroundColor = UIColor.white

var documentName: String = "test"

if let documentURL = Bundle.main.url(forResource: documentName, withExtension: "pdf") {
    if let document = PDFDocument(url: documentURL) {
        pdfView.displayDirection = .vertical
        pdfView.usePageViewController(true, withViewOptions: nil)
        pdfView.document = document
    }
}

self.view.addSubview(pdfView)

It displays fine, but the displayDirection is horizontal, not vertical. I'm trying to find a way to set the withViewOptions, but I can't find relevant info on this.

I checked on SO, and many people suggest to add displayDirection but that doesn't see to change anything.

On the other hand, when I try to run the following code:

pdfView = PDFView(frame: view.frame)
pdfView.backgroundColor = UIColor.white

var documentName: String = "test"

if let documentURL = Bundle.main.url(forResource: documentName, withExtension: "pdf") {
    if let document = PDFDocument(url: documentURL) {
        pdfView.autoScales = true
        pdfView.displayDirection = .vertical
        pdfView.displayMode = .singlePage
        pdfView.document = document
    }
}

self.view.addSubview(pdfView)

It just displays the first page and it doesn't scroll. I need to add swipe gestures recognizers and the scroll is not smooth. Beside, it seems overkill for a single page scrolling functionality...

Am I missing something? Any help would be greatly appreciated!

Upvotes: 3

Views: 2052

Answers (3)

Devrim Catak
Devrim Catak

Reputation: 66

In this case, DisplayMode = .SinglePage, but this also causes problems with multiple pages, so the displayMode should be taken by the number of pages and changed accordingly.

if pdfView.document!.pageCount == 1 {
     pdfView.displayMode = .singlePage
}

Upvotes: 0

Diogo Antunes
Diogo Antunes

Reputation: 2291

The only thing you need to do is use the following method from PDFView:

pdfView.usePageViewController(true, withViewOptions: [:])

PDFView-PageViewController

Upvotes: 2

Steven Brown
Steven Brown

Reputation: 21

Try pdfView.displayMode = .singlePageContinuous

Upvotes: 2

Related Questions