在iOS 5上,UIWebView报告滚动视图的内容大小错误 [英] On iOS 5, UIWebView is reporting wrong content size for scroll view

查看:91
本文介绍了在iOS 5上,UIWebView报告滚动视图的内容大小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一堆解决方案但是现在我正试图弄清楚UIWebView的滚动视图的内容大小。它目前总是返回1024,这是设备的宽度。这是没有意义的,因为我正在查询高度,并且视图是纵向的。

I have tried a bunch of "solutions" but for right now I am trying to figure out the content size of the scrollview of a UIWebView. It is currently always returning 1024 which is the width of the device. This doesn't make sense since I am querying the height and the view is in portrait orientation.

以下代码报告高度为1024.00000

The following code reports the height as 1024.00000

-(void)webViewDidFinishLoad:(UIWebView *)webView {

  float sourcesWebViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];

  NSLog(@"%f", sourcesWebViewHeight);

}

但我只有几行文字。

推荐答案

我将分解最终为我解决的问题。

I'll break down what finally solved this for me.

我必须将我的内容包装在其中。

I had to wrap my content in this.

<html><head><meta name=\"viewport\" content=\"initial-scale=1, user-scalable=no, width=device-width\" /></head><body>%@</body></html>

实现以下视图加载。

-(void)webViewDidFinishLoad:(UIWebView *)webView {

  [self layoutSubviews];

  webView.scrollView.scrollEnabled = NO;    // Property available in iOS 5.0 and later
  CGRect frame = webView.frame;


  frame.size.height = 1;        // Set the height to a small one.

  webView.frame = frame;       // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).

  frame.size.height = webView.scrollView.contentSize.height;  // Get the corresponding height from the webView's embedded scrollView.

  webView.frame = frame;





}

-(void) layoutSubviews {
  [super layoutSubviews];
  [body stringByEvaluatingJavaScriptFromString:
   [NSString stringWithFormat:
    @"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",
    (int)body.frame.size.width]];
}

最后我的webview会做所有正确的事情来扩展自己的内容。

and finally my webview does all the right things to scale itself to the content.

这篇关于在iOS 5上,UIWebView报告滚动视图的内容大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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