在IOS 5 UIWebView报告滚动视图的内容大小错误

我已经尝试了一堆“解决scheme”,但现在我正在试图找出UIWebView的滚动视图的内容大小。 目前总是返回1024这是设备的宽度。 这没有任何意义,因为我查询的高度和视图是纵向的。

下面的代码报告高度为1024.00000

-(void)webViewDidFinishLoad:(UIWebView *)webView { float sourcesWebViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue]; NSLog(@"%f", sourcesWebViewHeight); } 

但是我只有几行文字,这对我没有任何意义。 🙁

我会打破最终解决这个问题。

我不得不把我的内容包裹在里面。

 <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做所有正确的事情来扩展自己的内容。