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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5763986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:42:01  来源:igfitidea点击:

iPhone - Auto resizing UIWebView content do not fit the frame

iosiphoneuiwebviewframeautoresize

提问by Oliver

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.

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

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

我想让它的内容适合 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*/];

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.

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

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 ?

如果我将 webview 框架大小设置为整个 superview 的大小,就可以了。
如何强制网页内容(真正的 www 内容)适合“减少的” UIWebView 的框架?

回答by Nick Weaver

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

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

Look at the following screenshots:

看看下面的截图:

Screeshot 1, scalesPageToFit YES and NO

Screeshot 1,scalesPageToFit YES 和 NO

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

两个 webview 具有相同的宽度。在较低的页面中,页面非常适合。

enter image description here

在此处输入图片说明

Screeshot 2, scalesPageToFit both YES but smaller widths set

Screeshot 2, scalesPageToFit 两个 YES 但设置较小的宽度

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

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

enter image description here

在此处输入图片说明

回答by Rajni Johar Raswant

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];

回答by zshcbka

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

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

self.webView.scrollView.scrollEnabled = NO;

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

并添加一个单独的scrollView,它将处理滚动和缩放而不是webView's native scroll。下一个执行

- (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 webViewframe. Hope this helps someone. Good coding!

设置适当的webView框架。希望这可以帮助某人。好编码!

回答by cruzier

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

使用sizethatfits()中这将解决您的问题web视图。

回答by swiftBoy

For Swift 2.2I have achieved the same with

对于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)

回答by Lion

For Swift 3, I have achieved the same with

对于 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)
                }
            }

回答by salo.dm

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.

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

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

这个属性还有其他选项,你会在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.

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

Hope this is helpful.

希望这是有帮助的。

回答by Pavel Kanapatski

Try this:

尝试这个:

[oneView setScalesPageToFit:YES];

... instead of:

... 代替:

oneView.scalesPageToFit = YES;

Hope that helps.

希望有帮助。