如何让我的UIWebView内容适合屏幕并添加缩放比例?

时间:2015-09-12 06:26:16

标签: ios swift pdf uiwebview

我希望UIWebView中的内容适合屏幕,这是我的UIWebView中PDF的当前状态:

enter image description here

这就是我想要的样子,适合屏幕,每页边对边:

enter image description here

我的UIWebView也不允许我放大或缩小以缩放页面,我该如何解决这两个问题呢?

这是我的代码:

class PdfViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var myWebView: UIWebView!

    @IBOutlet var progressView: UIProgressView!

    var hasFinishedLoading = false


    override func viewDidLoad() {
        super.viewDidLoad()

        myWebView.delegate = self

        let url: NSURL! = NSURL(string: contentUrlPassedOn)
        myWebView.loadRequest(NSURLRequest(URL: url))

        self.myWebView.userInteractionEnabled = false
        self.myWebView.scalesPageToFit = true
        myWebView.contentMode = UIViewContentMode.ScaleAspectFit
        myWebView.frame = self.view.frame
        myWebView.scrollView.bouncesZoom = true

    }

    func webViewDidStartLoad(webView: UIWebView) {
        var hasFinishedLoading = false

        updateProgress()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
            dispatch_get_main_queue()) {
                [weak self] in
                if let _self = self {
                    _self.hasFinishedLoading = true
            }
        }
    }

    deinit {
        myWebView.stopLoading()
        myWebView.delegate = nil
    }

    func updateProgress(){
        if progressView.progress >= 1 {
            self.progressView.fadeOut()
            self.imageView.fadeOut()
            self.backgroundImageView.fadeOut()
            self.titleLabel.fadeOut()
            self.myWebView.userInteractionEnabled = true
        } else {
            if hasFinishedLoading {
                progressView.progress += 0.002
            } else {
                if progressView.progress <= 0.3 {
                    progressView.progress += 0.004
                } else if progressView.progress <= 0.6 {
                    progressView.progress += 0.002
                } else if progressView.progress <= 0.9 {
                    progressView.progress += 0.001
                } else if progressView.progress <= 0.94 {
                    progressView.progress += 0.0001
                } else {
                    progressView.progress = 0.9401
                }
            }

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.008 * Double(NSEC_PER_SEC))),
                dispatch_get_main_queue()) {
                    [weak self] in
                    if let _self = self {
                        _self.updateProgress()
                    }
             }
      }
}

谢谢

1 个答案:

答案 0 :(得分:0)

更新您的viewDidLoad(),如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    self.myWebView.delegate = self
    self.myWebView.scalesPageToFit = true
    //self.myWebView.frame = self.view.frame
    //self.myWebView.contentMode = UIViewContentMode.ScaleAspectFit
    //self.myWebView.userInteractionEnabled = false
    //self.myWebView.scrollView.bouncesZoom = true

    let url: NSURL! = NSURL(string: self.contentUrlPassedOn)
    self.myWebView.loadRequest(NSURLRequest(URL: url))
}

并在使用前声明URL字符串。

@IBOutlet var myWebView: UIWebView!
@IBOutlet var progressView: UIProgressView!
var hasFinishedLoading = false
var contentUrlPassedOn:String! 

确保在.xibstoryboard

中将插座设置为完美