Ios 以编程方式创建约束时出错
我有两个视图:aView和topView。这两者都具有在interface builder中生成的所有必要约束 现在我需要向视图中添加一个WKWebView实例。所以我创建了一个网页属性。然后我尝试初始化该对象,并向其添加4个约束,如下所示:Ios 以编程方式创建约束时出错,ios,xcode,autolayout,nslayoutconstraint,Ios,Xcode,Autolayout,Nslayoutconstraint,我有两个视图:aView和topView。这两者都具有在interface builder中生成的所有必要约束 现在我需要向视图中添加一个WKWebView实例。所以我创建了一个网页属性。然后我尝试初始化该对象,并向其添加4个约束,如下所示: self.webPage = [[WKWebView alloc] init]; NSLayoutConstraint *topConstraint =[NSLayoutConstraint
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.
我不明白我做错了什么。您需要将TranslatesAutoResizengMaskinToConstraints设置为false
self.webPage.translatesAutoresizingMaskIntoConstraints = false