PDFView not updating to reflect changes in document number of pages.

After creating a PDFView and setting it's document property, if I update the document by inserting a new page. The total number of pages in the document correctly updates, but the change is not reflected in the PDFView.


For example if the PDFView's PDFDocument is 8 pages. If I insert a new page at the end, i can partially see the new page at the end, but can't scroll past page 8, I can partially see the page 9, but not the whole thing.

Likewise if I insert the new page at the front (index 0), then I can see it there on the first page, but now I can only partially see the last page of the original document.


Either way the PDFView is not allowing full scrolling to anything beyond the original number of pages. Even though document.pageCount includes the new pages.


I tried `pdfView.layoutDocumentView()` (which seemed the most likey answer), `layoutSubview()`, removing the PDFView object from the superview and adding it again.


If I `write()` the new file out and open in preview it has all the new pages as expected.

Replies

@fromtoronto I had the same problem and found the following work around that will update the PDFView after the image is added as a new page.


if let imagePDFPage = PDFPage(image: image) {
     if let pdfDocument = pdfView.document {
          pdfDocument.insert(imagePDFPage, at: pdfDocument.pageCount)
          pdfView.document = nil
          pdfView.document = pdfDocument
     }
}

I have now learned that the correct solution is to use layoutDocumentView :-)


https://developer.apple.com/documentation/pdfkit/pdfview/1503757-layoutdocumentview?language=objc