之前項(xiàng)目需求一個(gè)view上面是web下面是原生,如何算出web高度布局下面視圖view.y。
eg:上面是新聞下面是評(píng)論,評(píng)論要用原生實(shí)現(xiàn)。
兩種方法,方法1可以得到內(nèi)容的實(shí)際高度,方法2得到了將內(nèi)容顯示完整后的 webView 的尺寸(包含 UIEdgeInsets)
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//方法1 實(shí)際使用js方法實(shí)現(xiàn)
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;
}
如果獲取高度不準(zhǔn)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), queue, ^{
寫上面介紹的方法
});