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

时间:2021-10-26 21:12:41

I want the content in my UIWebView to fit the screen, this is the current state of a PDF in my UIWebView:

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

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

This is how I want it to look, fit the screen, edge to edge each page:

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

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

And my UIWebView also doesn't let me zoom in or out to scale the pages, how do I fix both these issues?

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

Here is my code:

这是我的代码:

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()
                    }
             }
      }
}

Thank you

谢谢

1 个解决方案

#1


0  

Update your viewDidLoad() as following :

更新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))
}

and declare URL string before use.

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

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

Make sure outlet is set perfect in .xib or storyboard

确保在.xib或storyboard中设置完美的插座

#1


0  

Update your viewDidLoad() as following :

更新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))
}

and declare URL string before use.

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

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

Make sure outlet is set perfect in .xib or storyboard

确保在.xib或storyboard中设置完美的插座