I am using PDFKit as PDFViewer for my app, but I am facing an issue while viewing PDFs. I can't scroll the page, but on the bottom side, when I press double tap, it scrolls to the bottom, or we can say the next page content. one more thing: I am using the code below for Kotlin MultiPlatform.
import UIKit
import PDFKit
import composeApp
class IOSNativeViewFactory: NativeViewFactory {
func createPdfView(path: String) -> UIView {
let filePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.first?.appendingPathComponent(path)
let pdfView = PDFView()
pdfView.autoScales = true // Enables pinch-to-zoom
pdfView.displayMode = .singlePageContinuous // Enables scrolling
pdfView.displayDirection = .vertical
pdfView.minScaleFactor = 1.0
pdfView.maxScaleFactor = 4.0 // Adjust zoom limits
pdfView.scaleFactor = 1.5
if let filePath {
if let document = PDFDocument(url: filePath) {
pdfView.document = document
}
}
return pdfView
}
}
I am using PDFKit as PDFViewer for my app, but I am facing an issue while viewing PDFs. I can't scroll the page, but on the bottom side, when I press double tap, it scrolls to the bottom, or we can say the next page content. one more thing: I am using the code below for Kotlin MultiPlatform.
import UIKit
import PDFKit
import composeApp
class IOSNativeViewFactory: NativeViewFactory {
func createPdfView(path: String) -> UIView {
let filePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.first?.appendingPathComponent(path)
let pdfView = PDFView()
pdfView.autoScales = true // Enables pinch-to-zoom
pdfView.displayMode = .singlePageContinuous // Enables scrolling
pdfView.displayDirection = .vertical
pdfView.minScaleFactor = 1.0
pdfView.maxScaleFactor = 4.0 // Adjust zoom limits
pdfView.scaleFactor = 1.5
if let filePath {
if let document = PDFDocument(url: filePath) {
pdfView.document = document
}
}
return pdfView
}
}
Share
Improve this question
edited Mar 19 at 7:45
Sazzadhusen Iproliya
asked Mar 18 at 5:48
Sazzadhusen IproliyaSazzadhusen Iproliya
99415 silver badges19 bronze badges
1 Answer
Reset to default 0Found the Solution:
When you create a view controller in the same view. It will stick to PDF view on the same page and face a scrolling issue. But When you pass a reference of the parent view controller and add in the parent view controller, it works fine.
Below is the Code I have used in KMM.
func loadPdf(modifier: any UiModifier, name: String)-> Any {
let safeAreaInset = uiViewController.view.safeAreaInsets
let frame = CGRect(x: 0, y: safeAreaInset.top + 85, width: uiViewController.view.bounds.width, height: uiViewController.view.bounds.height-safeAreaInset.bottom)
pdfView = PDFView(frame: frame)
uiViewController.view.addSubview(pdfView)
let filePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.first?.appendingPathComponent(name)
if let filePath {
if let document = PDFDocument(url: filePath) {
pdfView.document = document
}
}
pdfView.translatesAutoresizingMaskIntoConstraints = false
pdfDocument = PDFDocument(url: filePath ?? URL(fileURLWithPath: ""))!
pdfView.document = pdfDocument
pdfView.minScaleFactor = 1.0
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
pdfView.displayDirection = .vertical
totalPAgeCount = 0
if let total = pdfView.document?.pageCount {
totalPAgeCount = total
}
countPages()
return uiViewController
}