How to control page display to PDF loaded in UIWebView for iOS (go to page)

前端 未结 5 827

I\'m developing an app which displays a PDF embedded in a WebView this is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *urlremoto = [NS         


        
5条回答
  •  醉梦人生
    2020-12-30 16:21

    Its late but may be helpful,

    For iOS less than 11.0

    func webViewDidFinishLoad(_ webView: UIWebView)
        {
    
            var pdfPageHeight:CGFloat = 0
    
            let a = webView.scrollView.subviews
    
            for var view in a
            {
                if view.isKind(of: NSClassFromString("UIWebPDFView")!)
                {
                    let b = view.subviews
    
                    for var iview in b
                    {
                        if iview.isKind(of: NSClassFromString("UIPDFPageView")!)
                        {
                            pdfPageHeight = iview.bounds.size.height
    
                            break
                        }
                    }
                }
            }
    
            webView.scrollView.setContentOffset(CGPoint(x: 0, y: CGFloat((pagenumber - 1)   + 1 )*pdfPageHeight), animated: true)
    
        }
    

    For iOS 11 and greater

    var pdfdocumentURL: URL?
    
        @IBOutlet weak var titleLbl: UILabel!
        @IBOutlet weak var pdfview: PDFView!
        var pagenumber = 1
    
    
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
            self.titleLbl.text = titleStr
    
           let pdfdocument = PDFDocument(url: pdfdocumentURL!)
    
            pdfview.document = pdfdocument
            pdfview.displayMode = PDFDisplayMode.singlePageContinuous
            pdfview.autoScales = true
    
            pagenumber = pagenumber - 1
            if let page = pdfdocument?.page(at: pagenumber) {
                pdfview.go(to: page)
            }
    
    
        }
    

提交回复
热议问题