Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios WKWebView不断突破限制?_Ios_Objective C_Iphone_Autolayout - Fatal编程技术网

Ios WKWebView不断突破限制?

Ios WKWebView不断突破限制?,ios,objective-c,iphone,autolayout,Ios,Objective C,Iphone,Autolayout,我创建了一个带有两个不同部分的UIViewController。有一个headerView和一个contentView,我想在其中添加一个WKWebView实例 由于我是以编程方式创建WKWebView,因此我必须以类似的方式添加约束 下面是我如何添加它们的: -(void)loadYoutubeVideoWithID:(NSString *)videoID { if (![self webView]){ /* Create WebView */ WKWe

我创建了一个带有两个不同部分的
UIViewController
。有一个
headerView
和一个
contentView
,我想在其中添加一个
WKWebView
实例

由于我是以编程方式创建
WKWebView
,因此我必须以类似的方式添加约束

下面是我如何添加它们的:

-(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)]];
    }
}
尽管增加了限制,但我无法让它们得到尊重。基于StackExchange中的其他答案,我尝试了四种不同的约束,但我的
WKWebView
在屏幕旋转时从不调整大小

我不知道如何解决这个问题。我已经链接了关于打破约束的输出日志(相当长),但它对我没有多大用处

有人知道为什么我无法调整
WKWebView

谢谢你抽出时间


编辑:当使用常规UIImageView代替WKWebView时,这也会发生。解决此问题相当简单。它要求您添加以下行:

[webView设置TranslatesAutoResizingMaskintoConstraints:否]

初始化
WKWebView
实例后,或在内容视图中要调整大小的任何其他
UIView
。下面是一个固定的示例:

-(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)]];
    }
}
我希望其他人能发现这一点