iOS:在 didFinishLoad 之后调整 UIWebView 的大小(内容应该适合而不滚动)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13858290/
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 21:32:22  来源:igfitidea点击:

iOS: Resize UIWebView after didFinishLoad (content should fit without scrolling)

iosxcodeuiwebviewheight

提问by filou

I am absolutely unable to resize my UIWebView.

我绝对无法调整我的UIWebView.

My UIWebViewis declared in .h and in connected in IB. The height in IB is 110. But the height of my text is about 1500. Now I'd like to change the height so the full text is readable without scrolling.

MyUIWebView在 .h 中声明,在 IB 中连接。IB 中的高度是 110。但我的文本高度约为 1500。现在我想更改高度,以便无需滚动即可阅读全文。

What code does resize my UIWebViewin webViewDidFinishLoad? Can't find ANYTHING that works.

什么代码不调整我UIWebViewwebViewDidFinishLoad?找不到任何有效的东西。

回答by John Stephen

The UIScrollViewproperty is available for UIWebViewstarting in iOS 5. You can use that to resize your view by implementing the webViewDidFinishLoad:message in your delegate, like this:

UIScrollView属性可用于UIWebView从 iOS 5 开始。您可以使用它通过webViewDidFinishLoad:在您的委托中实现消息来调整视图大小,如下所示:

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect newBounds = webView.bounds;
    newBounds.size.height = webView.scrollView.contentSize.height;
    webView.bounds = newBounds;
}

回答by zero3nna

Have you tried: this answer?

你有没有试过:这个答案

[Edit]

[编辑]

Some guys say this is working:

有些人说这是有效的:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

// Change the height dynamically of the UIWebView to match the html content
CGRect webViewFrame = webView.frame;
webViewFrame.size.height = 1;
webView.frame = webViewFrame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
// webViewFrame.size.width = 276; Making sure that the webView doesn't get wider than 276 px
webView.frame = webViewFrame;

float webViewHeight = webView.frame.size.height;
}

This is untested by me but 70 people voted ^ to this answer and its recommend to set sizeTahtFits:CGSizeZerolike a reset of your frame.size

这是我未经测试的,但 70 人对该答案投了 ^ 票,并建议将其设置sizeTahtFits:CGSizeZero为重置 frame.size

and some people use JavaScript to fix this:

有些人使用 JavaScript 来解决这个问题:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
CGFloat height = [string floatValue] + 8;
CGRect frame = [_webView frame];
frame.size.height = height;
[_webView setFrame:frame];

if ([[self delegate] respondsToSelector:@selector(webViewController:webViewDidResize:)])
{
    [[self delegate] webViewController:self webViewDidResize:frame.size];
}
}

回答by Maniac One

as answered by john and eric above, this piece of code works.

正如上面的 john 和 eric 所回答的,这段代码有效。

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect newBounds = webView.bounds;
    newBounds.size.height = webView.scrollView.contentSize.height;
    webView.bounds = newBounds;
}

however while trying with this code i found out it returns me with a proper webview but improper origin, if thats the case for anyone, simply reset with this code

然而,在尝试使用此代码时,我发现它以正确的 webview 但不正确的来源返回给我,如果任何人都是这种情况,只需使用此代码重置

webView.frame=CGRectMake(webView.frame.origin.x, webView.center.y, webView.frame.size.width, webView.frame.size.height);

The y-axis is set and you get the webView as you wanted.

y 轴已设置,您可以根据需要获得 webView。