// Inject新的attribute給你的wkwebview
- (void)setupWebView {
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('initial-scale', '1'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:userScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = userContentController;
// 因為pdf的高度用webview的寬度去調整,所以要先設定好。
CGRect initialWebViewFrame = self.view.frame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat height = initialWebViewFrame.size.height;
CGFloat width = initialWebViewFrame.size.width;
if (height > width) {
initialWebViewFrame.size = CGSizeMake(width, 0);
} else {
initialWebViewFrame.size = CGSizeMake(height, 0);
}
}
_webView = [[WKWebView alloc] initWithFrame:initialWebViewFrame configuration:wkWebConfig];
_webView.scrollView.contentSize = initialWebViewFrame.size;
[_webViewContainer addSubview:_webView];
_webView.navigationDelegate = self;
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
_webView.scrollView.scrollEnabled = NO;
[_webView.scrollView setBackgroundColor:[UIColor clearColor]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
} else {
if (shouldReloadFlag) {
shouldReloadFlag = NO;
[self loadWebView];
}
}
[self resizeScrollView];
}
- (void)resizeScrollView {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
if (_webView.isLoading == false) {
for (UIView *subview in self.webView.scrollView.subviews) {
subview.backgroundColor = [UIColor clearColor];
// 取得pdf的高
if ([[[subview class] description] isEqualToString:@"WKPDFView"]) {
NSLog(@"pdfview.size: %@ webviewContainer.size:%@", NSStringFromCGSize(subview.frame.size), NSStringFromCGSize(_webViewContainer.frame.size));
[self resizeScrollViewByWebViewContentHeight:subview.frame.size.height / subview.frame.size.width * _webViewContainer.frame.size.width];
break;
}
}
}
} else {
if (_webView.isLoading == false) {
__weak typeof(self) weakSelf = self;
[_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id result, NSError * _Nullable error) {
CGFloat height = [result floatValue] + 30.0;
[weakSelf resizeScrollViewByWebViewContentHeight:height];
}];
}
}
}
沒有留言:
張貼留言