标签:
项目中遇到这样的问题:webview放在uiscrollview,webview加载本地html,之后需要计算webview高度。
步骤如下:
1.首先创建webview.几点注意,高度一定要小于你要加载的html的高度,最好设定个最小值,比如1;并且设置scalesPageToFit = YES使之自适应高度;
self.bottomWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.middleView3.frame) + kSectionInterval, kScreenWidth, 100)]; self.bottomWebView.layer.borderColor = self.topView.layer.borderColor; self.bottomWebView.layer.borderWidth = self.topView.layer.borderWidth; self.bottomWebView.scalesPageToFit = YES;
2.其次在控制器中显示。设置代理,加载h5
self.recordScrollView.bottomWebView.delegate = self; [self loadH5WithId:self.check.checkId]; [self.view addSubview:self.recordScrollView];
3.加载h5
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"cj%d.htm", (int)checkId + 1] ofType:nil]; NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSURL *baseUrl = [NSURL fileURLWithPath:path]; [self.recordScrollView.bottomWebView loadHTMLString:htmlString baseURL:baseUrl];
4.代理方法中计算高度
- (void)webViewDidFinishLoad:(UIWebView *)webView{ float webViewHeight = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] floatValue]; webView.frame = CGRectMake(CGRectGetMinX(webView.frame),CGRectGetMinY(webView.frame),CGRectGetWidth(webView.frame),webViewHeight); webView.scrollView.scrollEnabled = NO; float heightSum = CGRectGetMinY(webView.frame) + webViewHeight + 15;//15增量 self.recordScrollView.contentSize = CGSizeMake(kScreenWidth, heightSum); }
这样就可以自适应高度了。
标签:
原文地址:http://www.cnblogs.com/xiangli/p/4710139.html