Ios 匹配子视图';它的宽度';使用自动布局创建超级视图

Ios 匹配子视图';它的宽度';使用自动布局创建超级视图,ios,uiwebview,uiscrollview,autolayout,Ios,Uiwebview,Uiscrollview,Autolayout,目标: 使用自动布局约束,使UIWebView与其superview(即UIScrollView)的宽度相同 代码 NSLayoutConstraint *makeWidthTheSameAsScrollView =[NSLayoutConstraint constraintWithItem:self.questionWebView

目标:

使用自动布局约束,使
UIWebView
与其superview(即
UIScrollView
)的宽度相同

代码

NSLayoutConstraint *makeWidthTheSameAsScrollView =[NSLayoutConstraint
                                                       constraintWithItem:self.questionWebView
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:0
                                                       toItem:self.masterScrollView
                                                       attribute:NSLayoutAttributeWidth
                                                       multiplier:1.0
                                                       constant:0];

[self.view addConstraint:makeWidthTheSameAsScrollView];

 NSLog(@"The width of questionWebView *AFTER* adding the constrain is: %f", self.questionWebView.frame.size.width);
 NSLog(@"The width of scrollView *AFTER* adding the constrain is: %f", self.masterScrollView.frame.size.width);
当前结果

当我记录
self.questionWebView
(UIWebView)的宽度时,当应用自动布局约束时,其宽度不会改变

问题

  • 这是正确的方法吗
  • 我做错了什么

p、 s我知道在
uicrollview
中放置
UIWebView
违反了苹果的建议,但是我已经关闭了使用属性
self.questionWebView.userInteractionEnabled=NO滚动
UIWebView
的功能。目前使用UIWebView是我显示HTML表的最佳策略

滚动视图与自动布局的交互方式有点奇怪。请参见(UIScrollView和Autolayout)

另见


通常,您需要以“scrollview的当前宽度”以外的其他方式获取包含视图的宽度,因为在自动布局中,scrollview的宽度(即内容宽度)是根据其内容定义的。因此,您当前的请求是循环的。

根据请求改进Rob的回答

正如Rob已经提到的,
UIScrollView
在自动布局下具有特殊的行为

在这种情况下,有趣的是scrollView总宽度是通过使用其子视图总宽度确定的。因此,当scrollView已经向webView询问宽度时,您告诉webView也向scrollView询问宽度。这就是它不起作用的原因。一个人在问另一个人,没有人知道答案。您需要另一个引用视图作为webView的约束,然后scrollView也将能够成功询问其预期宽度

一个简单的方法是:创建另一个视图,
containerView
,并将scrollView作为子视图添加到该视图中。然后为
containerView
设置适当的约束。假设您希望scrollView以viewController为中心,并在边缘添加一些填充。因此,对
容器视图执行以下操作:

NSDictionary *dict = NSDictionaryOfVariableBindings(containerView);
[self.view addConstraints:[NSLayoutConstraints constraintsWithVisualFormat:@"H|-(100)-[containerView]-(100)-|" options:0 metrics:0 views:dict];
[self.view addConstraints:[NSLayoutConstraints constraintsWithVisualFormat:@"V|-(100)-[containerView]-(100)-|" options:0 metrics:0 views:dict];
然后,您可以继续将webView作为子视图添加到scrollView并设置其宽度:

NSLayoutConstraint *makeWidthTheSameAsScrollView =[NSLayoutConstraint
                                                       constraintWithItem:self.questionWebView
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:0
                                                       toItem:containerView
                                                       attribute:NSLayoutAttributeWidth
                                                       multiplier:1.0
                                                       constant:0];

[self.view addConstraint:makeWidthTheSameAsScrollView];

这将使scrollview与webView一样大和高,并且它们都将按预期放置(在containerView上设置约束)。

一个简单的策略是将scrollview作为子视图添加到另一个视图中(布局与scrollview相同)并将“自动布局”设置为使用此视图的宽度来设置webView。@AlohaSilver谢谢,请将此作为单独的答案添加,我认为将其分解为答案而不是注释很有用。您也可以在IB中执行此操作:“在滚动视图中创建锚定视图”