Reputation: 853
I'm working on a PDF Viewer using new PDFKit in ios11. I have a long 90 page pdf document as my test document. Everything seems to be working fine re loading, viewing, scrolling, and navigating the document. I added a segmented control to the navigation bar with 2 segments which the user can tap to swap the pdfView displayMode from singlePageContinuous to twoUpContinuous and that works fine by itself too. The problem occurs when the pdf loads and I then pinch to zoom on the pdfView before tapping the segmentedControl to change displayMode ... the combination of pinching to zoom out, then tapping on the segmentedControl to change to say twoUpContinuous makes the pdfView lose track of the page within the document and it ends up showing me the last pages ie pages say 87,88,89, and 90 of the document. tapping the segmentedControl again to go back to singlePageContinuous results in me still at the end of the document.
Ive tried setting PDFView observers on page change when I scroll, saving that page to a local var and then calling pdfView.go(to: currentVisiblePage) etc to try and circumvent this but somehow the pdfView loses track of the correct page as the segmentedControl is tapped.
Some of my code
private func createPDFView() {
// Configure pdfView
pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
// Position the pdf view
pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
// Configure the toolView (created in storyboard)
view.bringSubview(toFront: toolView)
pdfThumbnailView.backgroundColor = Constants.Colors.collectionViewBackground
view.bringSubview(toFront: pdfThumbnailView)
private func addPageObserver() {
//Wednesday, 31 January 2018 trying to get the page number from the notifciation but not easy
pageObserver = NotificationCenter.default.addObserver(forName: Notification.Name.PDFViewPageChanged, object: nil, queue: OperationQueue.main) { [weak self] (notification) in
print("In pageObserver - currentVisiblePage is : \(String(describing: self?.pdfView.currentPage))")
self?.currentVisiblePage = self?.pdfView.currentPage
}
}
@IBAction func segmentedControlTapped(_ sender: UISegmentedControl) {
// Handle 2 or 4 button displayMode segmented control
switch sender.numberOfSegments {
case 2:
// 2 button case
switch sender.selectedSegmentIndex {
case 0:
pdfView.displayMode = .singlePageContinuous
case 1:
pdfView.displayMode = .twoUpContinuous
default:
pdfView.displayMode = .singlePageContinuous
}
default:
break
}
// Re-layout the document to re-centre if document zoomed by pinch before changing displayMode
pdfView.layoutDocumentView()
//
// Get current page from local var and set to try and fix pagination error
if let currentVisiblePage = currentVisiblePage {
print("in seg control handler ... goint to page: \(String(describing: currentVisiblePage))")
pdfView.go(to: currentVisiblePage)
}
}
Again I'll say there is no problem at all if zooming is not done before i change displayMode. Any help or pointers appreciated
Upvotes: 1
Views: 2073
Reputation: 853
It seems as though pagination is thrown out in a PDFView if scaleFactor is not at 1.0. If you've scaled your page to say 0.5, then trying to change pageDisplay or trying to navigate to the next page using PDFKit convenience methods seems to give an incorrect result ie takes you to an incorrect page within the document. I had to reset scaleFactor to 1.0 before doing any page manipulation for it to work properly.
Upvotes: 0