iOS UIWebView 无法确定web页面的真实高度

@interface IndEditorDetailViewController ()<UIWebViewDelegate,  NJKWebViewProgressDelegate>
{
    UIWebView *_webView;
    UIProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
}
@end

@implementation IndEditorDetailViewController

- (void)viewDidLoad {
    _progressView = [[UIProgressView alloc] init];
    _progressView.frame = CGRectMake(0, 0, 320, 10);
    [self.view addSubview:_progressView];
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;

}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
 // 一般获取网页高度会在这个方法里获取
 // 但这里获取的高度未必就是web页面的真实高度,因为web中图片未加载完有可能导致web界面不真实,或长或短
 // 你能可能用到下面的js方法去获取,但其实都没用
    /**
     1、document.documentElement.offsetHeight;
     2、document.body.clientHeight;
     3、document.documentElement.scrollHeight
     4、
         CGRect frame = webView.frame;
         CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
         frame.size = fittingSize;
         webView.frame = frame;
     5、getBodyHeight() // 前端js写好获取webView高度方法getBodyHeight()
     6、CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.clientHeight"] floatValue];
     */
  
  /** 所以必须要确定web彻底加载完,在网上找了个工具 NJKWebViewProgress,可以显示webView进度,在进度完成时,在计算webView高度即可!**/


}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    if (progress == 0.0) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        _progressView.progress = 0;
        [UIView animateWithDuration:0.27 animations:^{
            _progressView.alpha = 1.0;
        }];
    }
    
    if (progress == 1.0) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [UIView animateWithDuration:0.27 delay:progress - _progressView.progress options:0 animations:^{
            _progressView.alpha = 0.0;
            
            // webView彻底加载完
            CGFloat height = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.clientHeight"] floatValue];
            CGRect frame = _webView.frame;
            frame.size.height = height;
            _webView.frame = frame;
            
        } completion:nil];
    }
    
    [_progressView setProgress:progress animated:NO];
    
}

@end

你可能感兴趣的:(iOS UIWebView 无法确定web页面的真实高度)