Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal Scroll to display PDF in Swift

Tags:

pdf

swift

webview

i have collection view users can download and view the PDF file in web view. My app is landscape so vertical scroll feels weird, so i want to make the scroll horizontal. Is that possible in Web view? or i must use scrollview instead? in swift.

i'm using alamofire download and my destination:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]

                    let fileURL = documentsURL.URLByAppendingPathComponent("\(magazineObject.title).pdf")

                    return fileURL
                }

here's my code to view pdf

class RedirectMagazineViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

var receiveData: NSURL!
var receiveTitle: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    print("receiveData 2ndVC =>\(receiveData)")
    self.navigationController?.navigationBarHidden = false
    self.title = receiveTitle
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    let filePath = "\(documentsPath)/\(receiveTitle).pdf"
    let url = NSURL(fileURLWithPath: filePath)
    let urlRequest = NSURLRequest(URL: url)
    webView.loadRequest(urlRequest)
    activityIndicator.startAnimating()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
like image 722
Ariel Gemilang Avatar asked Dec 21 '25 17:12

Ariel Gemilang


1 Answers

In iOS 11 has Apple solved the problem with PDFKit.

A good intro can be found here

import PDFKit

@IBOutlet var pdfView: PDFView!

if let pdfDocument = PDFDocument(url: url) {
    pdfView.displayMode = .singlePageContinuous
    pdfView.autoScales = true
    pdfView.displayDirection = .horizontal
    pdfView.document = pdfDocument
}
like image 144
Morten Holmgaard Avatar answered Dec 24 '25 07:12

Morten Holmgaard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!