我已经尝试了很多“解决方案”,但现在我正在尝试计算 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;
}
and
-(void) layoutSubviews {
[super layoutSubviews];
[body stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:
@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",
(int)body.frame.size.width]];
}
最后,我的网络视图做了所有正确的事情来根据内容进行缩放。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)