iOS PDFKit not filling UIView
Asked Answered
N

5

9

I'm using PDFKit to display simple PDF's. When I display them on the screen inside a UIView, I've used the code below.

Aims: - Start off with the content to fill the view - want to fill whole screen width wise

override func viewDidLoad() {
    super.viewDidLoad()

    if let path = Bundle.main.path(forResource: "Disclaimer", ofType: "pdf") {
        let url = URL(fileURLWithPath: path)
        if let pdfDocument = PDFDocument(url: url) {
            pdfView.document = pdfDocument
            pdfView.autoScales = true
            pdfView.maxScaleFactor = 4.0
            pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
            pdfView.displayMode = .singlePageContinuous
            pdfView.displaysPageBreaks = false
        }
    }
    self.view.backgroundColor = UIColor.black
}

In storyboard I have set a constraint for UIview to fill full width of the screen - which it does.

Using autoscale sales the PDF document smaller than the width of the screen, it basically adds a margin around the pdf. I can zoom in and make he Pdf fill the whole screen, before it overflows and scroll bars come into play.

If I set the scale factor manually, I can make a single page PDF fill the screen width wise, but if the PDF has more than one page, then the width reverts to being less than the screen width with a margin present.

Ultimately I just want to fill the whole of the screen width wise with the PDF without any margin / gap.

Screenshot

Would appreciate some help.

UPDATE: Code used as per advice below -but presently not working:

if let path = Bundle.main.path(forResource: pdfObject, ofType: "pdf") {
            let url = URL(fileURLWithPath: path)
            if let pdfDocument = PDFDocument(url: url) {

            pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: pdfView.frame.width, height: pdfView.frame.height))
                pdfView.document = pdfDocument

            }

        }

Note: pdfView is my UIView that is the PDFView

Nichol answered 17/10, 2018 at 21:32 Comment(1)
As an addition I've tried (to no effect): pdfView.autoresizesSubviews = true pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]Nichol
N
12

Right, firstly many thanks to Mahesh. While his answer didn't directly answer my question, two things they said did provide a route to solving this.

Overall to set the pdf to fill the view, I needed to set autoresizing and autoscales BEFORE

pdfView.document = pdfDocument

And to then control the zoom, set these AFTER

pdfView.document = pdfDocument

Thus my final code is:

func pdfDisplay() {

    if let path = Bundle.main.path(forResource: pdfObject, ofType: "pdf") {
        let url = URL(fileURLWithPath: path)
        if let pdfDocument = PDFDocument(url: url) {

            pdfView.autoresizesSubviews = true
            pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
            pdfView.displayDirection = .vertical

            pdfView.autoScales = true
            pdfView.displayMode = .singlePageContinuous
            pdfView.displaysPageBreaks = true
            pdfView.document = pdfDocument

            pdfView.maxScaleFactor = 4.0
            pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit

        }
    } 
}

Not going to lie, I'm not sure why this works in this order, I would appreciate a comment from a wiser mind than mine but I hope this helps others

Nichol answered 28/10, 2018 at 20:0 Comment(5)
Nicholas Farmer can u check following example for me and tel me solution for me, here I wants to do same. Link -github.com/MallikarjunH/TestPDFFullScreen1.gitSoftwood
In my code/requirement displaying single page is needed i.e pdfView.displayMode = .singlePageSoftwood
Dear MK, sorry I haven’t time to download and review. Post as a separate question with all codeNichol
Nicholas Farmer - here is new question link - #66219528Softwood
Thank you so much, it worked for me finally after hours of trials.Belgravia
R
4

I have been struggling with a similar issue, and very weird and inconsistent behavior of autoScales. In my case, the automatic scaling worked either on the first loaded document, but not on any subsequent loaded documents in the same pdfView, or it only worked from the second loaded document.

I have embedded the pdfView in a UIScrollView, in order to gain more control over the gesture behavior.

The ordering of the commands seems to indeed be key. After a lot of trial and error, this is the order that worked for me best:

  1. Load the document

    pdfView.document = pdfDocument

  2. Set the display mode

    pdfView.displayMode = .singlePage

  3. Set any constraints, background, shadow etc.

  4. Perform changes to the pdfView size by modifying constraints

  5. Set autoScales

    pdfView.autoScales = true

  6. Set the scale factors

    pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit

This is the only order in which everything worked in my case, and it only worked when stages 2, 5 and 6 were all present.

Reiss answered 11/3, 2019 at 22:4 Comment(2)
Hey I want to display my pdf in fix size view.Freeswimming
Hi Ekta, have you considered putting the PDFView inside a custom UIScrollView? This is what I did, and I got much more control over it.Reiss
A
2

Just use pdfview = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)) before pdfView.document = pdfDocument

And remove these lines :

pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
Antisyphilitic answered 26/10, 2018 at 11:8 Comment(6)
Thanks for the input, unfortunately this doesn't work. I just get a blank screen (below the title). FYI my view hierarchy in Storyboard is: View - PDFViewNichol
please share you storyboard hierarchy.Antisyphilitic
if you are using UIView and inside that pdfview then you can change pdfview = PDFView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)) to pdfview = PDFView(frame: CGRect(x: 0, y: 0, width: yourView.frame.width, height: yourView.frame.height))Antisyphilitic
Hi Mahesh, apologies for delayed repose, I couldn't get to my computer. Unfortunately that doesn't work. I had actually tried it before posting comment above, but re-tired and no effect. Ive also tried removing constraints from pdfView to no effect. I would appreciate any further hep, I'm keen to give you the bounty!Nichol
@MaheshShahane Hey can you please help me ?Freeswimming
@EktaPadaliya what you need? let me know.Antisyphilitic
M
2

Setup the pdfView in viewDidLoad() and put this in viewDidLayoutSubviews():

pdfView.maxScaleFactor = 4.0 
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
Melamie answered 1/12, 2019 at 19:29 Comment(0)
L
0

First, set minScaleFactor, then set scaleFactor, which must be between minScaleFactor and maxScaleFactor.

Run a debug, and vary this values until you got the perfect size for you.

func showPDF(_ dataStr:String){
    guard let data = Data(base64Encoded: dataStr) else { return }
    let pdfView = PDFView(frame: self.view.bounds)
    pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    pdfView.autoScales = true
    pdfView.displayMode = .singlePageContinuous
    pdfView.displaysPageBreaks = true
    pdfView.document = PDFDocument(data:data)
    pdfView.maxScaleFactor = 4.0
    pdfView.minScaleFactor = 1.15
    pdfView.scaleFactor = 1.15
    self.view.addSubview(pdfView)
}
Liquidity answered 28/1, 2020 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.