以编程方式创建约束时出错
Error when creating constraints programmatically
我有 2 个视图:aView 和 topView。这两个都具有在界面构建器中进行的所有必要约束。
现在我需要向视图中添加一个 WKWebView 实例。所以我创建了一个网页 属性。然后我尝试初始化这个东西并像这样向它添加 4 个约束:
self.webPage = [[WKWebView alloc] init];
NSLayoutConstraint *topConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: self.topBar
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem: self.aView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:50.0];
NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: self.aView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem: self.aView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
[self.aView addSubview: self.webPage];
[self.aView addConstraints: @[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint]];
所以我的 WKWebView 实例总是在 topView 的正下方,比 aView.bottom 高 50 pt,并且粘在视图的边缘。
但我收到错误:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
我不明白我做错了什么。
您需要将 translatesAutoresizingMaskIntoConstraints 设置为 false
self.webPage.translatesAutoresizingMaskIntoConstraints = false
我有 2 个视图:aView 和 topView。这两个都具有在界面构建器中进行的所有必要约束。
现在我需要向视图中添加一个 WKWebView 实例。所以我创建了一个网页 属性。然后我尝试初始化这个东西并像这样向它添加 4 个约束:
self.webPage = [[WKWebView alloc] init];
NSLayoutConstraint *topConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: self.topBar
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem: self.aView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:50.0];
NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: self.aView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
constraintWithItem: self.webPage
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem: self.aView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
[self.aView addSubview: self.webPage];
[self.aView addConstraints: @[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint]];
所以我的 WKWebView 实例总是在 topView 的正下方,比 aView.bottom 高 50 pt,并且粘在视图的边缘。
但我收到错误:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
我不明白我做错了什么。
您需要将 translatesAutoresizingMaskIntoConstraints 设置为 false
self.webPage.translatesAutoresizingMaskIntoConstraints = false