当网页高度计算不准确时,尝试在网页加载完后多次调用

来源:互联网 发布:德军总部新秩序知乎 编辑:程序博客网 时间:2024/05/16 10:21

方法调用顺序为从上至下,第二个方法会调用多次,这样才能确保在加载完毕网页数据后正确的计算出网页主体部分的高度

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [selfsetWebViewHtmlImageFitPhone];

    

    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.clientHeight"]floatValue];

    

    //     //防止死循环

    if (height !=_viewModel.htmlHeight) {

        _viewModel.htmlHeight = height;

        

        if (_viewModel.htmlHeight >0) {


            NSLog(@"页面高度为:%ld",_viewModel.cellHeight);

        }

    }

    

}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    if ([keyPathisEqualToString:@"contentSize"]) {

        CGFloat height = [[self.webViewstringByEvaluatingJavaScriptFromString:@"document.documentElement.clientHeight"]floatValue];

        

        //     //防止死循环

        if (height !=_viewModel.htmlHeight) {

            //           _viewModel.otherHeight = 533;

            _viewModel.htmlHeight = height;

            

            if (_viewModel.htmlHeight >0) {

                

                NSLog(@"页面高度为:%ld",_viewModel.cellHeight);

                

                

            }

        }

    }

}

阅读全文
0 0
原创粉丝点击