WKWebView更改大小问题

时间:2018-09-19 12:57:41

标签: ios wkwebview nslayoutconstraint uiviewanimation

我有一个带有WKWebView的控制器(它位于容器视图内,因为WKWebView在iOS 10之前无法使用)。 我想使用动画来更改WKWebView的大小,但是当WKWebView的大小发生更改时,加载的网页将无法查看,并且无法根据新的Web视图大小进行调整。

这是我的代码:

此代码是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;
}

我错了吗?

1 个答案:

答案 0 :(得分:0)

您需要将webView clipToBounds的属性设置为true。 Web视图的框架缩小为您指定的大小,但内容不会被裁剪。因此,您需要将内容剪切为。

self.webView.clipToBounds = true