简体   繁体   English

设置自动布局约束会导致“无法同时满足约束”错误

[英]Setting autolayout constraints causes 'Unable to simultaneously satisfy constraints' error

First time I set AutoLayout constraints of a view using code (I need a dynamic view to be created in different viewControllers and I prefer to create this UIView "on the fly"). 首次使用代码设置视图的AutoLayout约束(我需要在不同的viewControllers中创建动态视图,并且我希望“动态”创建此UIView)。

My UIView should be attached to the superview top, as large as the superview and the bottom should be attached to the top of a particular view (navigationbar). 我的UIView应该附加到超级视图的顶部,与超级视图一样大,并且底部应该附加到特定视图的顶部(导航栏)。

I tried the following: 我尝试了以下方法:

let newView = UIView()
newView.backgroundColor = UIColor.red
view.addSubview(newView)

newView.translatesAutoresizingMaskIntoConstraints = false

let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)

let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view,attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)

let bottomConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: navigationBar, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)

NSLayoutConstraint.activate([verticalConstraint, widthConstraint, bottomConstraint])

I get the following runtime error but I can't understand what's the constraint that is not useful: I have a manually created view with identical constraints (there's one more too) and I get no errors: 我收到以下运行时错误,但我不明白没有什么约束是有用的:我有一个手动创建的视图,该视图具有相同的约束(也有一个约束),并且没有错误:

[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. 
(
    "<NSLayoutConstraint:0x60800009d060 UINavigationBar:0x7f8061c0d2b0.top == UILayoutGuide:0x6080001ace80'UIViewSafeAreaLayoutGuide'.top   (active)>",
    "<NSLayoutConstraint:0x60400008fc80 UIView:0x7f8061f13b20.centerY == UIView:0x7f8061c17ce0.centerY   (active)>",
    "<NSLayoutConstraint:0x60400008faa0 UIView:0x7f8061f13b20.bottom == UINavigationBar:0x7f8061c0d2b0.top   (active)>",
    "<NSLayoutConstraint:0x60000009b120 'UIView-Encapsulated-Layout-Height' UIView:0x7f8061c17ce0.height == 667   (active)>",
    "<NSLayoutConstraint:0x60800009d0b0 'UIViewSafeAreaLayoutGuide-top' V:|-(20)-[UILayoutGuide:0x6080001ace80'UIViewSafeAreaLayoutGuide']   (active, names: '|':UIView:0x7f8061c17ce0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60400008fc80 UIView:0x7f8061f13b20.centerY == UIView:0x7f8061c17ce0.centerY   (active)>

Any help is appreciated 任何帮助表示赞赏

Just add top, leading, trailing constraints to the custom view with respect to its superview. 只需在自定义视图的上级视图上添加顶部,前导,尾随约束即可。 Add the bottom constraint to the top another view that will lie below your custom view & is also a subview on your custom view's superview. 将底部约束添加到顶部,另一个视图将位于自定义视图的下方,并且也是自定义视图的超级视图的子视图。

In your code the problem is with your bottom constraint as well. 在您的代码中,问题还在于您的底部约束。 You are trying to add the constraint b/w the bottom of custom view & top of navigation bar. 您正在尝试在自定义视图的底部和导航栏的顶部添加约束b / w。

 let topConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)

let leadingConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)

let trailingConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)

let bottomConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: anotherView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)

You don't need to use a constraint for the centerY property. 您不需要对centerY属性使用约束。 Since you aim is to pin the view to the top, try using a topAnchor constraint and make it equal to superview.topAnchor. 由于您的目标是将视图固定在顶部,因此请尝试使用topAnchor约束并将其等于superview.topAnchor。 Hope this helps: 希望这可以帮助:

let newView = UIView()
newView.backgroundColor = UIColor.red
view.addSubview(newView)

newView.translatesAutoresizingMaskIntoConstraints = false

let topConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.topAnchor, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.topAnchor, multiplier: 1, constant: 0)

let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view,attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)

let bottomConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: navigationBar, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)

NSLayoutConstraint.activate([verticalConstraint, widthConstraint, bottomConstraint])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM