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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值