Ariel Gemilang
Ariel Gemilang

Reputation: 791

Horizontal Scroll to display PDF in Swift

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.
}
}

Upvotes: 3

Views: 2474

Answers (3)

Morten Holmgaard
Morten Holmgaard

Reputation: 7806

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
}

Upvotes: 1

steipete
steipete

Reputation: 7641

UIWebView is very limited in what it allows you to customize when it comes to displaying PDF. Enabling the vertical scroll indicators will not change anything.

The only way to switch to vertical scrolling is to start all the way from scratch at CGContextDrawPDFPage - see Apple's documentation for a start

There are various open source and commercial PDF SDK available that solve this, as it's a very hard problem to even just replicate the features of UIWebView. Text selection alone is really, really hard. I've been working on PSPDFKit since 2010, and we're still working on it 7 years later.

Apple's ZoomingPDFViewer is a good intro if you are willing to invest a few month to build this yourself.

Upvotes: 0

Rizwan Shaikh
Rizwan Shaikh

Reputation: 2904

PDF files are inherently a collection of pages as soon as you do not design pdf file that support horizontal manner you could not horizontal scroll the pdf file Regard less you display pdf in UIWebView, Chrome, Safari, Preview, FireFox, Windows 8 Reader etc..`

Upvotes: 0

Related Questions