webview自动适应高度的方法
2016-07-15 17:09
260 查看
/////////////////////////////初始化,self.view是父控件///////////////////////////////// _webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 0)]; _webView.delegate = self; _webView.scrollView.bounces = NO; _webView.scrollView.showsHorizontalScrollIndicator = NO; _webView.scrollView.scrollEnabled = NO; [_webView sizeToFit]; ///////////////////////////////设置内容,这里包装一层div,用来获取内容实际高度(像素),htmlcontent是html格式的字符串////////////// NSString * htmlcontent = [NSString stringWithFormat:@"<div id=\"webview_content_wrapper\">%@</div>", htmlcontent]; [_webView loadHTMLString:htmlcontent baseURL:nil]; ////////////////////////////////delegate的方法重载//////////////////////////////////////////// - (void)webViewDidFinishLoad:(UIWebView *)webView { //获取页面高度(像素) NSString * clientheight_str = [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]; float clientheight = [clientheight_str floatValue]; //设置到WebView上 webView.frame = CGRectMake(0, 0, self.view.frame.size.width, clientheight); //获取WebView最佳尺寸(点) CGSize frame = [webView sizeThatFits:webView.frame.size]; //获取内容实际高度(像素) NSString * height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top')) + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"]; float height = [height_str floatValue]; //内容实际高度(像素)* 点和像素的比 height = height * frame.height / clientheight; //再次设置WebView高度(点) webView.frame = CGRectMake(0, 0, self.view.frame.size.width, height); }
相关文章推荐
- Android 启动后台运行程序(Service)
- IOS tabBarController的tabBarItem显示图片
- android可发中使用的十六进制颜色转换布局显示颜色
- Android中ListView分页加载更多数据
- Android 资源名获取R文件id
- iOS des加密转码16进制字符串
- ios开发-获取手机相关信息
- 如何获取android app的Activity
- android proguard使用心得和遇到的若干问题以及解决思路
- android camera 根据加速器的变化实现自动对焦
- IOS代理
- Android ActionBar使用笔记
- Android.9图详解
- Quick_Cocos基础(三)
- iOS textField限制输入框长度,包括判断汉字总结
- iOS开发工具——统计Crash的工具Crashlytics
- Android library projects cannot be launched解决方法
- android悬浮窗口的实现
- iOS开发 - 让tableView不能下拉刷新,可以上拉加载
- android实现动画自动播放的渐变欢迎页面