简体   繁体   English

WKWebView更改大小问题

[英]WKWebView change size issue

I have a controller with WKWebView (its inside a container view because WKWebView can't use before iOS 10). 我有一个带有WKWebView的控制器(它在容器视图内,因为WKWebView在iOS 10之前无法使用)。 I want to change the WKWebView's size using animation but when WKWebView's size changed the webpage loaded goes off to view and its not adjust with the new web view size. 我想使用动画来更改WKWebView的大小,但是当WKWebView的大小发生更改时,加载的网页将无法查看,并且无法根据新的Web视图大小进行调整。

this is my code: 这是我的代码:

this code is the webView's controller 这段代码是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];
}

this is the code about animation: 这是关于动画的代码:

-(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;
}

what I'm wrong? 我错了吗?

You need to set the property of webView clipToBounds to true. 您需要将webView clipToBounds的属性设置为true。 Your web view's frame is reduced to the size you specified but the content is not clipped. Web视图的框架缩小为您指定的大小,但内容不会被裁剪。 So you need to clip the content as. 因此,您需要将内容剪切为。

self.webView.clipToBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM