xcode UIWebView 内部 UIScrollView 内容大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14111139/
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
UIWebView Inside UIScrollView Content Size
提问by Jessica
I have to add UIWebView
and some buttons and labels inside UIScrollView
. I have seen many question on how to add UIWebView
inside UIScrollView
but unfortunately they were unable to help me complete my task. So kindly don't mark it as duplicate or something.
我必须在里面添加UIWebView
一些按钮和标签UIScrollView
。我已经看到很多关于如何在UIWebView
里面添加的问题,UIScrollView
但不幸的是他们无法帮助我完成我的任务。所以请不要将其标记为重复或其他东西。
Theoretically, the problem can be solved by following steps:
理论上,该问题可以通过以下步骤解决:
1) Make UIWebView
a subview of UIScrollView
.
1)制作UIWebView
一个子视图UIScrollView
。
2) Call setScrollEnabled:NO
for UIWebView
to disable its native scrolling.
2)调用setScrollEnabled:NO
为UIWebView
禁用其本土滚动。
3) Set the content size of both UIScrollView
and UIWebView
to the size of the HTML string loaded inside UIWebview
.
3)设置两者的内容的大小UIScrollView
和UIWebView
到HTML字符串的大小加载内部UIWebview
。
I am using iOS 6 and StoryBoards. I have added UIWebView
as the subview of UIScrollView
.
我正在使用 iOS 6 和 StoryBoards。我已添加UIWebView
为UIScrollView
.
Then in my webViewDidFinishLoad
, I have used the following:
然后在我的webViewDidFinishLoad
,我使用了以下内容:
NSString *webHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.height;"];
NSLog(@"WebView Height %@", webHeight);
That gives me the height of the WebView.
这给了我 WebView 的高度。
On the StackOverFlow, I have come across the following.
在 StackOverFlow 上,我遇到了以下问题。
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
Then i have set the size of the UISCrollView
to the size UIWebView
:
然后我将 的大小设置UISCrollView
为大小UIWebView
:
mainScrollView.contentSize = webView.bounds.size;
I can scroll down to the bottom but there is a white space at the bottom and UIWebView
is not making its size according to the size of the loaded html content.
我可以向下滚动到底部,但底部有一个空白区域,并且UIWebView
没有根据加载的 html 内容的大小来调整其大小。
Looks like UIScrollView
has changed it size to the size of the content of UIWebView
but UIWebView
is not showing all of it. How can I solve this issue?
看起来UIScrollView
已将其大小更改为内容的大小,UIWebView
但UIWebView
并未显示全部内容。我该如何解决这个问题?
Kindly do not advice me about the Apple documentation that says you should not use UIWebView
inside UIScrollView
. I already know that but I want to use it as per my project requirement.
请不要就 Apple 文档中说你不应该使用UIWebView
inside向我提出建议UIScrollView
。我已经知道了,但我想根据我的项目要求使用它。
回答by Ed Chin
I think I can shed some light here. I think you are confusing the frame size and the content size. First I'll describe methodology, then make concrete suggestions.
我想我可以在这里说明一些问题。我认为您混淆了框架大小和内容大小。首先我将描述方法,然后提出具体的建议。
Methodology
方法
You have a webView and you correctly disabled the scrolling on it and set the contentSize to the full size of the web content. You should also set the frame size to the full size of the web content. Then you addSubview this to the scrollView at origin (0,0). Set the scrollView contentSize to the full size of the web content. Set the scrollView frame to the bounds of the viewController.view.
您有一个 webView 并且您正确禁用了它的滚动并将 contentSize 设置为 Web 内容的完整大小。您还应该将框架大小设置为 Web 内容的完整大小。然后将Subview this 添加到原点(0,0)处的scrollView。将 scrollView contentSize 设置为 Web 内容的完整大小。将 scrollView 框架设置为 viewController.view 的边界。
Concrete Suggestions
具体建议
- The "fittingSize" code you posted does not look right to me. The purpose of that code is to set the frame of the webView. Use NSLog or breakpoints to find out if that is setting the full size of the web content.
- Note that the purpose of the javascript code that you posted is the same as the "fittingSize" code that you posted. Check to see which one produces the correct output.
- Try to figure out the frame sizes of the webView and scrollView to help you debug. The best way to do that is to set different background colors like this:
setBackgroundColor:[UIColor redColor]
- My best guess is that the
webView.frame.size
is not being set large enough.
- 您发布的“fittingSize”代码在我看来不合适。该代码的目的是设置 webView 的框架。使用 NSLog 或断点来确定是否设置了 Web 内容的完整大小。
- 请注意,您发布的 javascript 代码的目的与您发布的“fitSize”代码相同。检查以查看哪一个产生正确的输出。
- 尝试弄清楚webView和scrollView的帧大小来帮助你调试。最好的方法是设置不同的背景颜色,如下所示:
setBackgroundColor:[UIColor redColor]
- 我最好的猜测是
webView.frame.size
设置的不够大。
回答by philions
I've found same situation that webView do not reflects frame size changing, a blank remains while scrolls down as Jessica mentioned. I checked my project and found I've enabled autoLayout feature, so solutions I've found are:
我发现同样的情况 webView 不反映帧大小的变化,正如杰西卡提到的那样,向下滚动时会留下空白。我检查了我的项目,发现我启用了 autoLayout 功能,所以我找到的解决方案是:
- disable autoLayout.
add height constraint to webView, after changing webView frame size,using follow code like:
self.webViewHeightConstraint.constant = fittingSize.height; [webView updateConstraints];
- 禁用自动布局。
在更改 webView 框架大小后,向 webView 添加高度约束,使用如下代码:
self.webViewHeightConstraint.constant = fitingSize.height; [webView 更新约束];
回答by Custom Bonbons
To get the height of a UIWebView's content, I highly recommend the answer by 'Phenomena' here: http://stackoverflow.com(Not the accepted answer, the one below.) He/she suggests NOT using 'CGSizeZero', and gives a better solution.
为了获得 UIWebView内容的高度,我强烈推荐这里“现象”的答案:http: //stackoverflow.com(不是接受的答案,下面的那个。)他/她建议不要使用“CGSizeZero”,并给出一个更好的解决方案。