Micrified
Micrified

Reputation: 3650

WKWebView continuously breaking constraints?

I've created a UIViewController with two distinct sections. There's a headerView and a contentView in which I want to add a WKWebView instance.

Since I'm creating the WKWebView programmatically, I've got to add the constraints in a likewise fashion.

Here's how I add them:

-(void)loadYoutubeVideoWithID:(NSString *)videoID {
    if (![self webView]){
        /* Create WebView */
        WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];

        /* Set Delegate */
        [webView setNavigationDelegate:self];

        /* Set Local Property */
        [self setWebView:webView];

        /* Add to content view */
        [self.contentView addSubview:webView];

        /* Create Constraints */
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
    }
}

Despite adding constraints, I can't get them to be respected. I've tried up to four different variations of these constraints based on other answers across StackExchange, but my WKWebView never resizes when the screen rotates.

Before

After

I'm not sure how to fix this. I've got the output log linked here (It's rather long) concerning the constraints breaking, but it's not of much use to me.

Does anyone know why I am unable to resize the WKWebView?

Thank you for your time.

Edit: This also happens with a regular UIImageView, when used in place of the WKWebView

Upvotes: 3

Views: 2939

Answers (1)

Micrified
Micrified

Reputation: 3650

Solving this problem is rather simple. It requires that you add the line:

[webView setTranslatesAutoresizingMaskIntoConstraints:NO];

After initializing your WKWebView instance, or any other UIView that you wish to resize within the content view. Here is the fixed example:

-(void)loadYoutubeVideoWithID:(NSString *)videoID {
    if (![self webView]){
        /* Create WebView */
        WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
        /* Ensure Constraints remain when resizing the View */
        [webView setTranslatesAutoresizingMaskIntoConstraints:NO];
        /* Set Delegate */
        [webView setNavigationDelegate:self];

        /* Set Local Property */
        [self setWebView:webView];

        /* Add to content view */
        [self.contentView addSubview:webView];

        /* Create Constraints */
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
    }
}

I hope others find this instructional.

Upvotes: 6

Related Questions