iPhone - 自动调整 UIWebView 内容大小不适合框架

发布于 2024-11-03 03:57:46 字数 751 浏览 4 评论 0原文

我正在 viewDidLoad 方法中生成一个 UIWebView,其尺寸很小(比如说 50x70)。然后我把它放入一个超级 UIView 中。

我想让它的内容适合 webView 框架。为此,我写道:

        oneView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, W, H)];
        oneView.backgroundColor = [UIColor whiteColor];
        oneView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        oneView.scalesPageToFit = YES;
        oneView.autoresizesSubviews = YES;

        [self.view addSubview:oneView];
        [oneView loadRequest:/*some request*/];

但是这样做,网页的大小不会调整为 UIWebView 的框架。 它似乎被缩放到其他东西,比超级视图的框架更小,并且比网络视图的框架更宽。

在此处输入图像描述

如果我将 webview 框架大小设置为整个超级视图的大小,就可以了。
我如何强制网页内容(真实的 www 内容)适合“缩小的” UIWebView 的框架?

I'm generating an UIWebView into my viewDidLoad method, with a tiny size (let's say something like 50x70). And then I put it into a super UIView.

I'd like to make its content fit the webView frame. To do this, I wrote :

        oneView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, W, H)];
        oneView.backgroundColor = [UIColor whiteColor];
        oneView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        oneView.scalesPageToFit = YES;
        oneView.autoresizesSubviews = YES;

        [self.view addSubview:oneView];
        [oneView loadRequest:/*some request*/];

But doing this, the web page is not resized to the frame of the UIWebView.
It seems to be scaled to something else, smaller than the superview's frame, and wider than the webview's one.

enter image description here

If I set the webview frame size to the whole superview's size, it's ok.
How may I force the web content (real www content) to fit the frame of the "reduced" UIWebView ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

揪着可爱 2024-11-10 03:57:46

答案是:你已经在这样做了,但是有限制。

看下面的截图:

截图 1,scalesPageToFit YES 和 NO

两个 webview 具有相同的宽度。下一页的页面非常适合。

在此处输入图像描述

Screeshot 2,scalesPageToFit 均为 YES,但设置了较小的宽度

两个 webview 都尝试适应页面,但不会,因为有大小限制。

在此处输入图像描述

The answer is: you are already doing this but there are limits.

Look at the following screenshots:

Screeshot 1, scalesPageToFit YES and NO

Both webview have the same width. In the lower one the page fits perfectly.

enter image description here

Screeshot 2, scalesPageToFit both YES but smaller widths set

Both webview try to fit the page but it won't as there is a size limit.

enter image description here

裂开嘴轻声笑有多痛 2024-11-10 03:57:46

试试这个。这对我有用。

NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];

[webView loadHTMLString:strTemplateHTML baseURL:nil];

Try this.That's working for me.

NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];

[webView loadHTMLString:strTemplateHTML baseURL:nil];
风苍溪 2024-11-10 03:57:46

对于那些遇到同样问题的人,您可以关闭 UIWebView 原生滚动

self.webView.scrollView.scrollEnabled = NO;

并添加一个单独的 scrollView 来处理滚动和缩放,而不是 webView 的原生滚动。
接下来实现

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
   CGRect frame = _webView.frame;
   CGSize fittingSize = [_webView sizeThatFits:_webView.scrollView.contentSize];
   frame.size = fittingSize;
   _webView.frame = frame;
   self.scrollView.contentSize = self.webView.scrollView.contentSize;
}

设置正确的 webView 框架。
希望这对某人有帮助。良好的编码!

For those, who faced same problem, you can turn off the UIWebView native scrolling by

self.webView.scrollView.scrollEnabled = NO;

and add a separate scrollView which will handle scrolling and zooming instead of webView's native scroll.
Next implement

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
   CGRect frame = _webView.frame;
   CGSize fittingSize = [_webView sizeThatFits:_webView.scrollView.contentSize];
   frame.size = fittingSize;
   _webView.frame = frame;
   self.scrollView.contentSize = self.webView.scrollView.contentSize;
}

to set proper webView frame.
Hope this helps someone. Good coding!

顾忌 2024-11-10 03:57:46

在 web 视图中使用 sizethatfits() 可以解决您的问题。

Use sizethatfits() in the webview which would resolve your issue.

深海不蓝 2024-11-10 03:57:46

您可以执行以下操作:

oneview.contentMode = UIViewContentModeScaleAspectFit;

这将使您的视图内容随着视图的增长而增长。内容将尽可能大,同时仍然适合 UIWebView 并且不变形。

此属性还有其他选项,您会在 iOS 视图编程指南

请注意,自动调整大小您设置的掩码将使视图本身仅在其超级视图增长时增长。如果在创建小 UIWebView 时 self.view 已经很大并且 self.view 没有增长,则 UIWebView 将不会增长。您可能已经意识到这一点,但我添加它只是为了以防万一,因为我们在这段代码中看不到 self.view 的框架。

希望这有帮助。

You can do the following:

oneview.contentMode = UIViewContentModeScaleAspectFit;

That will make the content of your view grow as your view grows. The content will grow as big as possible while still fitting within the UIWebView and without distortion.

There are other options for this property, and you will find a pretty good explanation of the more complicated ones, along with pictures showing the effect of each, in the View Programming Guide for iOS

Note that the autoresizing mask you set will make the view itself grow only when its superview grows. If self.view is already big when the small UIWebView is created and self.view does not grow, the UIWebView won't be growing. You're probably aware of this, but I'm adding it just in case, since we can't see self.view's frame in this snippet of code.

Hope this is helpful.

懒的傷心 2024-11-10 03:57:46

对于 Swift 2.2 我已经实现了相同的目标

//Document file url
var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")

let req = NSURLRequest(URL: docUrl!)
webView.delegate = self
//here is the sole part
webView.scalesPageToFit = true
webView.contentMode = .ScaleAspectFit
webView.loadRequest(req)

For Swift 2.2 I have achieved the same with

//Document file url
var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")

let req = NSURLRequest(URL: docUrl!)
webView.delegate = self
//here is the sole part
webView.scalesPageToFit = true
webView.contentMode = .ScaleAspectFit
webView.loadRequest(req)
七婞 2024-11-10 03:57:46

对于 Swift 3,我已经实现了同样的目标

DispatchQueue.main.async {
                if let finalURL = URL(string: urlString) {
                    let request = URLRequest(url: finalURL)

                    // self.webView.sizeToFit()
                    self.webView.scalesPageToFit = true
                    self.webView.contentMode = .scaleAspectFit
                    self.webView.loadRequest(request)
                }
            }

For Swift 3, I have achieved the same with

DispatchQueue.main.async {
                if let finalURL = URL(string: urlString) {
                    let request = URLRequest(url: finalURL)

                    // self.webView.sizeToFit()
                    self.webView.scalesPageToFit = true
                    self.webView.contentMode = .scaleAspectFit
                    self.webView.loadRequest(request)
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文