iOS备忘录iOS技术交流收藏02-收集

iOS关于webView(一)—计算 webView 显示内容后

2016-03-29  本文已影响10486人  Kan__

之前项目需求一个view上面是web下面是原生,如何算出web高度布局下面视图view.y
eg:上面是新闻下面是评论,评论要用原生实现。

两种方法,方法1可以得到内容的实际高度,方法2得到了将内容显示完整后的 webView 的尺寸(包含 UIEdgeInsets)

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//方法1 实际使用js方法实现
CGFloat documentWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').offsetWidth"] floatValue];
CGFloat documentHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
NSLog(@"documentSize = {%f, %f}", documentWidth, documentHeight);

//方法2
CGRect frame = webView.frame;
frame.size.width = 768;
frame.size.height = 1;
//    webView.scrollView.scrollEnabled = NO;
webView.frame = frame;
frame.size.height = webView.scrollView.contentSize.height;
NSLog(@"frame = %@", [NSValue valueWithCGRect:frame]);
webView.frame = frame;
}

如果获取高度不准

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), queue, ^{
写上面介绍的方法
});

链接

计算 webView 显示内容后实际高度

改变UIWebView背景色

上一篇 下一篇

猜你喜欢

热点阅读