我有2个视图:aView和topView。这两个都具有在界面构建器中所做的所有必要约束。
现在我需要在视图中添加一个WKWebView实例。所以我创建了一个webPage属性。然后我尝试初始化并添加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.
我无法理解我做错了什么。
答案 0 :(得分:1)
您需要将translatesAutoresizingMaskIntoConstraints设置为false
self.webPage.translatesAutoresizingMaskIntoConstraints = false