UIWebView未完全加载,高度变化 [英] UIWebView Doesn't Load Completely, Height Varies

查看:80
本文介绍了UIWebView未完全加载,高度变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些标签的视图控制器和一个在其中加载一些html的UIWebView。所有这些标签和UIWebView都是UIScrollView的子视图。我已禁用UIWebView的滚动,因此标签和UIWebView在UIScrollView内一起滚动。

I have a view controller containing some labels and a UIWebView that is loading some html in it. All these labels and UIWebView are subviews of a UIScrollView. I have disabled the scrolling of UIWebView so the labels and UIWebView scrolls all together inside the UIScrollView.

然后我找到了UiWebView内部的内容大小,更改了UIWebView根据内容大小调整大小,然后将滚动视图的大小设置为webview的大小。这是我通过在webViewDidFinishLoad中添加以下代码来实现的:

Then i took found the size of the content inside the UiWebView, changed the UIWebView size according to the content size and then set the size of the scroll view to the size of the webview. This is how i did it by adding the following code in webViewDidFinishLoad:

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; 

当我使用NSLog查看webview内容的大小时,我注意到它给出了不同的结果相同内容的高度。例如,如果我第一次打开视图控制器,它会显示高度等于915.0,当我在导航中向后移动并返回到此视图控制器时,日志将显示大小为1012.0。

As i am using NSLog to see the size of the content of webview, i noticed that it gives different height for the same content. For example, if i open the view controller the first time, it shows the height equal to 915.0 and when i move back in the navigation and come back to this view controller, then the log will show the size to be 1012.0.

我认为这可能是由于html内容中的图片加载造成的。任何人都可以告诉我如何修复它以便第一次显示正确的高度?谢谢!

I believe it might be due to the images loading in the html content. Can anybody tell how can i fix it to show the right height the first time? Thanks!

更新:如果我使用javascript使用不同的策略,我已尝试使用以下代码:

UPDATE: If i use a different strategy using javascript, that i have already tried using the following code:

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;

此代码每次在日志中给出正确的高度,这正是我想要的但整个webview向上移动了一点,阻挡了它上面的所有标签。我做错了什么以及如何解决它?谢谢!

This code gives me the right height every time in the log which is exactly i want but the whole webview is shifted a little upward blocking all the labels above it. Am i doing something wrong and how to fix it? Thanks!

推荐答案

当您尝试在UIScrollView中嵌入UIWebView和其他视图时,这是一个非常常见的问题。

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

解决方案1:您已经解释了问题中的第一个解决方案。这个解决方案只有在UIScrollView中的UIWebView中只涉及文本时才有效。如果有图像和ajax或任何懒惰加载的东西,这个解决方案不会在第一次给你正确的高度。

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; 

解决方案2:另一种解决方案是使用javascript。它每次都给你正确的高度,但你将面临我无法排除故障的奇怪问题。在你的情况下,你说整个视图有点向上移动。

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;

解决方案3:最后一个解决方案是适用于我的解决方案它肯定会解决你的问题。它涉及合并我描述的前两个解决方案中的代码。也就是说,你将使用javascript获得高度,因为它每次都给出正确的高度,然后在第一个解决方案中使用其余代码并增加几点。注意我添加了+125.0,这就是我解决它的方法:

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;

希望这有帮助!

这篇关于UIWebView未完全加载,高度变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆