iOS WKWebView 计算高度的几种方法

[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

- (void)dealloc{

    [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize" context:nil];

}

- (void)observeValueForKeyPath:(NSString *)keyPath

                      ofObject:(id)object

                        change:(NSDictionary *)change

                       context:(void *)context

{

    if (object == self.webView.scrollView && [keyPath isEqual:@"contentSize"]) {

        // we are here because the contentSize of the WebView's scrollview changed.

//        UIScrollView *scrollView = self.webView.scrollView;

//        NSLog(@"方法五 ###### New contentSize: %f x %f", scrollView.contentSize.width, scrollView.contentSize.height);

//

//        CGSize fittingSize = CGSizeMake(kFullScreenWidth, scrollView.contentSize.height);

//        self.height = fittingSize.height;

//        self.webView.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height);

//        self.fileModel.level = 1; //加载完毕做标记;

//        [[NSNotificationCenter defaultCenter] postNotificationName:kSSRealQuestionWebCellNotificationName object:self];

        //[self.webView.scrollView removeObserver:self forKeyPath:@"contentSize" context:nil];

    }

}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation

{

    

    // 方法一

//    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error){

//        NSLog(@"方法三 ###### ======= offsetHeight 高度:%.2f",[result floatValue]);

//    }];

    NSLog(@"方法一 ###### web.End: %.2f",webView.scrollView.contentSize.height);

    CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];

    self.height = fittingSize.height;

    self.webView.frame = CGRectMake(0, 0, fittingSize.width,webView.scrollView.contentSize.height);

    self.fileModel.level = 1; //加载完毕做标记;

    [[NSNotificationCenter defaultCenter] postNotificationName:kSSRealQuestionWebCellNotificationName object:self];

//

//    // 方法二

//    CGFloat webViewHeight = 0.0;

//    if ([webView.subviews count] > 0){

//        UIView *scrollerView = webView.subviews[0];

//        if ([scrollerView.subviews count] > 0){

//            UIView *webDocView = scrollerView.subviews.lastObject;

//            if ([webDocView isKindOfClass:[NSClassFromString(@"WKPDFView") class]]){

//                webViewHeight = webDocView.frame.size.height;//获取文档的高度

//                //webView.frame= webDocView.frame; //更新UIWebView 的高度

//                NSLog(@"方法二 ###### WKPDFView Height: %.2f",webViewHeight);

//            }

//        }

//    }

//

//    // 方法三

//    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error){

//        NSLog(@"方法三 ###### ======= scrollHeight 高度:%.2f",[result floatValue]);

//    }];

//

//    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error){

//        NSLog(@"方法三 ###### ======= offsetHeight 高度:%.2f",[result floatValue]);

//    }];

//

//    [webView evaluateJavaScript:@"document.readyState" completionHandler:^(id _Nullable result,NSError * _Nullable error){

//        [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error){

//            NSLog(@"方法四 ###### ======= scrollHeight 高度:%.2f",[result floatValue]);

//        }];

//    }];

//

//

//    CGSize fittingSize = CGSizeMake(kFullScreenWidth, webView.scrollView.contentSize.height + 1300); // 高度计算不对

//

//    //CGSize fittingSize = CGSizeMake(kFullScreenWidth, 3430);

//

//    self.height = fittingSize.height;

//    self.webView.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height);

//    self.fileModel.level = 1; //加载完毕做标记;

//    [[NSNotificationCenter defaultCenter] postNotificationName:kSSRealQuestionWebCellNotificationName object:self];

}


猜你喜欢

转载自blog.csdn.net/qq_27247497/article/details/109166502