Search code examples
iosuiviewscalepdfkitpdfview

iOS PDFKit not filling UIView


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


Solution

  • 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