標籤:
@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實現好的方法 6、CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.clientHeight"] floatValue]; */ // 所以必須要找到能完全確定web載入,才能確定web高度 // 在網上找了個工具 NJKWebViewProgress}#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頁面的真實高度