简体   繁体   English

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

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

I am absolutely unable to resize my UIWebView . 我绝对无法调整UIWebView大小。

My UIWebView is declared in .h and in connected in IB. 我的UIWebView在.h中声明并在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. IB中的高度是110.但是我的文本高度约为1500.现在我想改变高度,以便在不滚动的情况下读取全文。

What code does resize my UIWebView in webViewDidFinishLoad ? 什么代码在webViewDidFinishLoad中调整我的UIWebViewwebViewDidFinishLoad Can't find ANYTHING that works. 找不到任何有效的东西。

The UIScrollView property is available for UIWebView starting in iOS 5. You can use that to resize your view by implementing the webViewDidFinishLoad: message in your delegate, like this: 从iOS 5开始, UIScrollView属性可用于UIWebView 。您可以通过在委托中实现webViewDidFinishLoad:消息来使用它来调整视图大小,如下所示:

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

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:CGSizeZero like 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];
}
}

as answered by john and eric above, this piece of code works. 正如上面的约翰和埃里克所回答的那样,这段代码可行。

-(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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM