У меня есть контроллер с WKWebView (он внутри контейнера, потому что WKWebView не может использовать до iOS 10).Я хочу изменить размер WKWebView с помощью анимации, но когда размер WKWebView изменился, загруженная веб-страница уходит на просмотр и не корректируется с новым размером веб-просмотра.
это мой код:
этокод контроллера webView
- (void)viewDidLoad
{
[super viewDidLoad];
self.activityIndicatorView.hidesWhenStopped = YES;
self.activityIndicatorView.hidden = YES;
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKWebViewConfiguration *webConfiguration = [WKWebViewConfiguration new];
WKUserContentController *contentController = [WKUserContentController new];
[contentController addScriptMessageHandler:self name:self.WKWebDataSource.messageString];
[contentController addUserScript:wkUScript];
webConfiguration.allowsInlineMediaPlayback = YES;
webConfiguration.userContentController = contentController;
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, self.container.frame.size.height) configuration:webConfiguration];
self.webView.scrollView.showsVerticalScrollIndicator = NO;
self.webView.scrollView.showsHorizontalScrollIndicator = NO;
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
self.webView.navigationDelegate = self;
[self.container addSubview:self.webView];
[self addConstraintToWKWebView];
NSURL *url = [[NSURL alloc] initWithString:MGPSafeString(self.WKWebDataSource.urlString)];
NSURLRequest *requestUrl = [[NSURLRequest alloc] initWithURL:url];
self.title = self.WKWebDataSource.titleString;
[self.webView loadRequest:requestUrl];
}
- (void) addConstraintToWKWebView
{
NSMutableArray <NSLayoutConstraint*> *arrConst = [NSMutableArray <NSLayoutConstraint*> new];
NSLayoutConstraint *topCons = [self.webView.topAnchor constraintEqualToAnchor:self.container.topAnchor];
NSLayoutConstraint *trailCons = [self.webView.trailingAnchor constraintEqualToAnchor:self.container.trailingAnchor];
NSLayoutConstraint *leadCons = [self.webView.leadingAnchor constraintEqualToAnchor:self.container.leadingAnchor];
NSLayoutConstraint *bottomCons = [self.webView.bottomAnchor constraintEqualToAnchor:self.container.bottomAnchor];
[arrConst addObject:topCons];
[arrConst addObject:trailCons];
[arrConst addObject:leadCons];
[arrConst addObject:bottomCons];
[NSLayoutConstraint activateConstraints:arrConst];
}
это код анимации:
-(void)viewAnimationWithFrame:(CGRect)frame duration:(CFTimeInterval)duration
{
CGRect layerNewFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
CGRect viewNewFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
self.webView.scrollView.scrollEnabled = NO;
self.container.layer.masksToBounds = YES;
[CATransaction begin];
[CATransaction setAnimationDuration:duration];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear]];
CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"frame"];
[self.view.layer setFrame:layerNewFrame];
[self.view.layer addAnimation:myAnimation forKey:@"AnimationStreamingController4"];
[self.webView.layer setFrame:layerNewFrame];
[self.webView.layer addAnimation:myAnimation forKey:@"AnimationStreamingController3"];
[CATransaction commit];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
self.view.frame = viewNewFrame;
self.container.frame = viewNewFrame;
self.webView.frame = viewNewFrame;
[self.view layoutIfNeeded];
[self.view setNeedsUpdateConstraints];
[self.webView layoutIfNeeded];
[self.webView setNeedsUpdateConstraints];
[UIView commitAnimations];
self.webView.scrollView.contentSize = viewNewFrame.size;
}
в чем я не прав?