Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 665
Micrified Avatar asked Mar 17 '26 08:03

Micrified


1 Answers

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.

like image 160
Micrified Avatar answered Mar 18 '26 22:03

Micrified