在我的应用程序中,我使用动画在导航控制器中推送视图控制器。
在它的viewDidLoad中我加载了一个本地html。问题是这个html呈现裁剪,但如果我开始滚动Web视图,html将获得正确的大小。
这是视图控制器的代码,viewDidLoad方法。
@implementation WebViewController_iPad
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *jScript = [NSString stringWithFormat:@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=%d'); document.getElementsByTagName('head')[0].appendChild(meta);", (int)self.mainContentView.frame.size.width];
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
self.webView = [[WKWebView alloc] initWithFrame:self.mainContentView.bounds configuration:wkWebConfig];
self.webView.navigationDelegate = self;
self.webView.scrollView.scrollEnabled = YES;
[self.mainContentView addSubview:self.webView];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
//WebView Contraints
NSLayoutConstraint *topWebView = [NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainContentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
NSLayoutConstraint *trailingWebView = [NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.mainContentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0];
NSLayoutConstraint *leadingWebView = [NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.mainContentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
//WebView and AdView related constraint s
NSLayoutConstraint *bottomWebView = [NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem: self.mainContentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
//Add WebView constraints
[self.mainContentView addConstraints:@[topWebView,trailingWebView,leadingWebView]];
//WebView and AidView related constraints
[self.mainContentView addConstraint:bottomWebView];
NSString *finalArticalBody = @"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ultrices quis enim ut euismod. Aenean urna felis, aliquam sit amet facilisis id, commodo in ante. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque vehicula nisi nulla, sed mattis turpis dictum tincidunt. Nunc ornare ipsum lorem, vitae placerat odio tempus vel. Mauris suscipit lorem a odio aliquam, ac congue nibh venenatis. Fusce eu ipsum iaculis nisl pharetra lacinia. Aliquam id aliquet sapien. Aenean tellus magna, vehicula id dapibus et, hendrerit in enim. Praesent tristique turpis sed urna blandit scelerisque. Nam venenatis nisl sem, volutpat finibus leo ullamcorper eget.</p>";
[self.webView loadHTMLString:finalArticalBody baseURL:nil];
}
动画代码是:
[self addSubview:viewController.view];
// set the frame to the right side of the last view controller
viewController.frame = CGRectMake(topVC.frame.origin.x + topVC.frame.size.width, 0, viewController.view.frame.size.width, self.frame.size.height);
if (animated)
{
// place off screen
viewController.view.left = MAX(initialWidth, self.contentSize.width) + 51;
viewController.view.height = self.frame.size.height;
[UIView animateWithDuration:delay
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
viewController.view.left = self.contentSize.width + 50;
} completion:^(BOOL finished) {
[UIView animateWithDuration:kStackViewAnimationDuration
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
// place on the right side of the last view controller
viewController.view.frame = viewController.frame;
} completion:^(BOOL finished) {
[self performAfterPush:viewController];
}];
}];
}
else
{
// find the x offset to place this new view controller
viewController.view.frame = viewController.frame;
[self performAfterPush:viewController];
}
我发现如果我将动画设置为false,则html从一开始就会获得正确的大小。
此外,如果我在演示文稿中设置动画并在viewDidLoad中加载0.5秒的延迟来加载html,则html的大小合适。
以前,我使用的是弃用的类UIWebView,但它没有这个问题。
为了让WKWebView html内容大小合适,我需要考虑什么?
错误如下所示:
如果我滚动网页视图,它会获得适当的大小。
请帮助!!