iOS - 强制 UIWebView 加载内容?
我使用 loadHtmlString:
设置 UIWebView
的内容。我不会将它们添加到 UIView
直到需要它们,但我需要知道它们每个的最大滚动高度。
问题是 UIWebView
在添加到 superView
之前不会开始加载其数据,并且加载内容的延迟导致了此问题。
问题:是否可以强制 UIWebView
加载其内容,而不将其添加到视图中?
I set the content of a UIWebView
using loadHtmlString:
. I don't add them to the UIView
until they are need, but I need to know the max scroll height of each of them.
The problem is that UIWebView
doesn't start loading it's data until it's added to a superView
, and It's the delay of loading content that is causing this problem.
Question: Is it possible to force a UIWebView
to load its content, without being added to a view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将其添加到视图中并将其不透明度设置为 0.01 以强制加载。我以前必须用 MKMapView 这样做。
You can add it to the view and set it's opacity to 0.01 to force the load. I've had to do this with MKMapView before.
我会使用
UIWebView
的hidden
属性 - 在添加视图之前将其设置为YES
并将其清除为NO
在webViewDidFinishLoad:
中应该可以解决问题。然而,您对 @logancautrell 的回答的评论有点令人不安 - 您是否计划将那么多网络视图添加到您的视图中?我假设您知道这一点,但以防万一:根据您可能的 HTML(5)/JavaScript/CSS 代码,即使 iOS 需要为其分配大量资源的少量 Web 视图,您也可能会观察到严重的性能影响。
如果您计划经常更新 Web 视图,也许您可以从 UIWebView 的
stringByEvaluatingJavaScriptFromString:
方法中受益?我认为,在某种程度上,性能受到的影响是可以忍受的。I'd use
hidden
property ofUIWebView
- setting it toYES
before view is added and clearing it toNO
inwebViewDidFinishLoad:
should do the trick.However, you comment to @logancautrell's answer is a bit unsettling - do you ever plan to get that many web views added to your views? I assume you know that, but just in case: depending on your possible HTML(5)/JavaScript/CSS code, you may observe severe performance hit even with a small number of web views for which iOS needs to allocate lots of resources.
If you plan to update your web views often, maybe you could benefit from UIWebView's
stringByEvaluatingJavaScriptFromString:
method? To some extent, the performance hit can be bearable, I think.我建议要么以某种方式预先计算滚动高度(不是在 Web 视图加载期间),要么找到一种方法来显示 UIViews,而不需要等待滚动高度,然后再纠正它们。
I suggest either pre-calculating the scroll heights somehow (not during web view load), or finding a way to display your UIViews without waiting for the scroll heights initially, and correct them later.