关于UIWebView设置高度自适应的问题

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 66, 320, 480)];
    
    //中间还有其他控件
    
//创建webView时要设置一个临时的frame,不然的话webViewDidFinishLoad代理方法中设置webView高度自适应之后,webView会有很多空白 _webView
= [[UIWebView alloc]initWithFrame:CGRectMake(0, 100, 320, 10)]; _webView.delegate = self; //_webView.ScalesPageToFit = YES;//可以用捏合手势来放大或缩小webView页面 _webView.scrollView.scrollEnabled = NO;//禁止UIWebView滚动 //WebView加载本地的html数据 NSMutableDictionary *dict = [LoadFileTools loadWebViewContentsWithFileName:kJobDescribe]; NSString *filePath = [dict objectForKey:@"filePath"]; NSString *htmlString = [dict objectForKey:@"htmlString"]; [_webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]]; } - (void)webViewDidFinishLoad:(UIWebView *)webView {

   //获取WebView内容的高度(貌似这两种方式都行)
   //NSString *contentH = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
   NSString *contentH = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];

    //计算坐标
    CGFloat webViewY = otherControlY + 50;
    CGFloat webViewH = [contentH floatValue] + 25;
    
    webView.frame = CGRectMake(12, webViewY, 320 - 12, webViewH);
    //禁止选中
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect=‘none‘;"];
    //禁止弹出菜单
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout=‘none‘;"];
    
    //设置滚动区域
    _scrollView.contentSize = CGSizeMake(0, webViewY + webViewH - 10);
    [_scrollView addSubview:_webView];
    [self.view addSubview:_scrollView];
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。