ios – How to set the top edge of a pdf in PDFKit in SwiftUI?


How do I set the top anchor point of a pdf in PDFKit? I am looking for the same behavior as iOS files app. When I tap the pdf I want the bars on top and bottom to slide away and the pdf to be full screen. The problem I am having is that the top of the pdf is unreadable do to the Dynamic Island. I want the top of the pdf to be where a navigation bar would end.

Here is the entire code file that I am using to render the pdf. I have modified some of the names but otherwise it’s the same.

class PDFViewController: UIViewController {
    let document: PDFDocument?
    var pdfView: PDFView!
    
    init(document: PDFDocument?) {
        self.document = document
        
        super.init(nibName: nil, bundle: nil)
    }
    
    override func loadView() {
        let view = PDFView()
        self.view = view
        self.pdfView = view
                
        view.document = document
        view.displayDirection = .vertical
        view.autoScales = true
    }
    
    required init?(coder: NSCoder) {
        document = nil
        
        super.init(coder: coder)
        return nil
    }
    
    override func viewDidLayoutSubviews() {
        let bounds = view.bounds
        if let document {
            if let page = document.page(at: 0) {
                let pageBounds = page.bounds(for: .mediaBox)
                if bounds.width > 0 && pageBounds.width > 0 {
                    let scaleFactor = bounds.width / pageBounds.width
                    let subtractionFactor = scaleFactor * 0.0125 // 0.008 for 6.1" iPhone
                    pdfView.minScaleFactor = scaleFactor - subtractionFactor
                }
            }
        }
    }
}

struct SheetView: UIViewControllerRepresentable {
    typealias UIViewControllerType = PDFViewController
    
    let document: PDFDocument?
    let file: AsyncFile?
    
    func makeUIViewController(context: Context) -> PDFViewController {
        if let file, document == nil {
            file.invalidate()
        }
        
        let controller = PDFViewController(document: document)
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: PDFViewController, context: Context) {
        
    }
}

Here is what I am trying to get:
Here is what I am trying to get:

And here is what I am currently getting:
And here is what I am currently getting:

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img