UIWebView Doesn't Load Completely, Height Varies

梦想的初衷 提交于 2019-11-28 19:01:30

This is a very common problem when you try to embed a UIWebView And Other views inside a UIScrollView.

SOLUTION 1: You have already explained the first solution in your question. How ever this solution is only valid when there is only text involved in the UIWebView inside the UIScrollView. If there are images and ajax or any such thing that lazily loads, this solution won't give you the right height the first time.

CGRect frame = myWebView.frame;
frame.size.height = 1;
myWebView.frame = frame;
CGSize fittingSize = [myWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
myWebView.frame = frame;

NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);

mainScrollView.contentSize = myWebView.bounds.size; 

SOLUTION 2: The other solution is using javascript. It gives you the right height everytime but you will face weird issues that i am unable to troubleshoot. In your case, you said that the whole view is shifted a little updwards.

CGRect oldBounds = [[self myWebView] bounds];
CGFloat height = [[myWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
NSLog(@"NEW HEIGHT %f", height);
[myWebView setBounds:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, oldBounds.size.width, height)];
mainScrollView.contentSize = myWebView.bounds.size;

SOLUTION 3: The last solution is the one that worked for me and it will definitely solve your problem. It involves merging the code in the first two solutions i described. That is you will get the height using javascript as it gives the right height everytime and then use the rest of the code in the first solution and increment few points. Notice i added +125.0, this is how i solved it:

CGFloat height = [[myWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
CGFloat width = [[myWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
CGRect frame = myWebView.frame;
frame.size.height = height + 125.0;
frame.size.width = width;
myWebView.frame = frame;
mainScrollView.contentSize = myWebView.bounds.size;

Hope this helps!

The WebView loads all its content asynchronously.

you have to wait for webViewDidFinishLoading so the html is loaded (not the image data yet!) Then it knows the size of the html. There sizeToFit should work.

nielsbot

Since UIWebView loads in the background, you will have to wait until it loads your page, then figure out the height.

One method is here: https://stackoverflow.com/a/3937599/210171.

However, I usually use the javascript technique. In your web view delegate, change your -webViewDidFinishLoad: to look like this:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString * heightString = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
    CGFloat contentHeight = [ heightString doubleValue ] ;
    NSLog( @"contentHeight=%f\n", contentHeight ) ;
}

(This code is taken from https://stackoverflow.com/a/751326/210171)

The height of the content may continue to change as images load if your images don't have their size attribute set. In that case, please see my answer here: https://stackoverflow.com/a/12030878/210171

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!