如何让我的UIWebView自动适应像WKWebView那样的内容?

时间:2021-02-24 21:12:16

I was comparing a UIWebView and a WKWebView to see which would be best to display PDF content. I realized that WKWebView's automatically scale the content to fit the screen with no code needed, like so:

我正在比较一个UIWebView和一个WKWebView,看看哪个最适合显示PDF内容。我意识到WKWebView会自动缩放内容以适应屏幕而不需要代码,如下所示:

[如何让我的UIWebView自动适应像WKWebView那样的内容?] 1[如何让我的UIWebView自动适应像WKWebView那样的内容?]2

[] 1 [] 2

While no matter what code I tried implementing in my UIWebView, the content just won't fit like the WKWebView does, here's what it looks like in my UIWebView:

虽然无论我尝试在我的UIWebView中实现什么代码,但内容都不适合像WKWebView那样,这就是我在UIWebView中的样子:

如何让我的UIWebView自动适应像WKWebView那样的内容?如何让我的UIWebView自动适应像WKWebView那样的内容?

and here is the code for my webView:

class WebView: UIViewController, UIWebViewDelegate, UIGestureRecognizerDelegate {

    @IBOutlet var myWebView: UIWebView!


    var contentUrlPassedOn: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        myWebView.delegate = self

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

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

    }

How can I get my UIWebView to fit content like the WKWebView?

如何让我的UIWebView适合像WKWebView这样的内容?

1 个解决方案

#1


0  

This is in Objective-C, but this is how I do it. If you translate it will work just fine.

这是在Objective-C中,但我就是这样做的。如果你翻译它会工作得很好。

[self.webView setScalesPageToFit:YES];

Perhaps, also removing this line would help do the trick. Not sure you really need it:

也许,删除这一行也有助于解决问题。不确定你真的需要它:

myWebView.contentMode = UIViewContentMode.ScaleAspectFit

Another idea is that you should set auto layout constraints to pin your UIWebView to the top and sides so that it won't resize to larger than the screen size.

另一个想法是你应该设置自动布局约束来将UIWebView固定到顶部和侧面,这样它就不会调整到大于屏幕大小。

Here is an example of how I would do this in Objective-C:

以下是我将如何在Objective-C中执行此操作的示例:

NSURLRequest*request=[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

[self.webView setScalesPageToFit:YES];

self.webView.delegate = self;

#1


0  

This is in Objective-C, but this is how I do it. If you translate it will work just fine.

这是在Objective-C中,但我就是这样做的。如果你翻译它会工作得很好。

[self.webView setScalesPageToFit:YES];

Perhaps, also removing this line would help do the trick. Not sure you really need it:

也许,删除这一行也有助于解决问题。不确定你真的需要它:

myWebView.contentMode = UIViewContentMode.ScaleAspectFit

Another idea is that you should set auto layout constraints to pin your UIWebView to the top and sides so that it won't resize to larger than the screen size.

另一个想法是你应该设置自动布局约束来将UIWebView固定到顶部和侧面,这样它就不会调整到大于屏幕大小。

Here is an example of how I would do this in Objective-C:

以下是我将如何在Objective-C中执行此操作的示例:

NSURLRequest*request=[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

[self.webView setScalesPageToFit:YES];

self.webView.delegate = self;