Ios 使用“代码获取错误”创建约束;“无法同时满足约束”;

Ios 使用“代码获取错误”创建约束;“无法同时满足约束”;,ios,swift,autolayout,constraints,Ios,Swift,Autolayout,Constraints,我在情节提要中添加了一个UIView(称为swipedView),并且没有对其设置任何自动布局约束。没有什么。现在在代码中,我想添加一些约束。为了测试它,我想把它固定在屏幕的两侧(所以它是全屏的) 当我运行它时,会收到一条很长的错误消息,比如: 2016-07-18 22:59:07.990 FindTheWay[87145:18423835] Unable to simultaneously satisfy constraints. Probably at least one of t

我在情节提要中添加了一个UIView(称为swipedView),并且没有对其设置任何自动布局约束。没有什么。现在在代码中,我想添加一些约束。为了测试它,我想把它固定在屏幕的两侧(所以它是全屏的)

当我运行它时,会收到一条很长的错误消息,比如:

2016-07-18 22:59:07.990 FindTheWay[87145:18423835] 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:0x7ff02d11a6e0 V:|-(0)-[UIView:0x7ff02b479a40]   (Names: '|':UIView:0x7ff02b450100 )>",
    "<NSIBPrototypingLayoutConstraint:0x7ff02b416a50 'IB auto generated at build time for view with fixed frame' V:|-(20)-[UIView:0x7ff02b479a40]   (Names: '|':UIView:0x7ff02b450100 )>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x7ff02b416a50 'IB auto generated at build time for view with fixed frame' V:|-(20)-[UIView:0x7ff02b479a40]   (Names: '|':UIView:0x7ff02b450100 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
我完全不熟悉通过代码设置约束,所以我不知道如何处理这个问题。我检查了在stackOverflow上发现的几个类似问题,但不幸的是,在这里没有任何帮助。

设置
swipedView
转换自动调整大小的GMaskintoConstraints
false
,而不是
查看

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var swipedView: UIView!
    @IBOutlet weak var foregroundView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // that was a proposed fix to my problem I found in the internet. Didn't work
        swipedView.translatesAutoresizingMaskIntoConstraints = false

    }

    override func viewDidAppear(animated: Bool) {

        let pinTop = NSLayoutConstraint(item: swipedView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0)
        let pinLeft = NSLayoutConstraint(item: swipedView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0)
        let pinRight = NSLayoutConstraint(item: swipedView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1.0, constant: 0)
        let pinBottom = NSLayoutConstraint(item: swipedView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0)

        NSLayoutConstraint.activateConstraints([pinTop, pinLeft, pinRight, pinBottom])

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

此解决方案最终起作用的原因可以在中找到

请注意,自动调整大小遮罩约束完全指定了视图的大小和位置;因此,在不引入冲突的情况下,无法添加其他约束来修改此大小或位置

translatesAutoResizengMaskintoConstraints
设置为
true
时,我们的意思是希望将相关视图的大小和位置转换为实际约束

通过将
translatesAutoResizengMaskintoConstraints
设置为
false
,我们可以说相关视图的大小和位置不应该因为任何原因而变成约束(在本例中,我们创建了自己的约束)

由于我们不希望应用约束来修改
视图的大小和位置,而实际上是
swipedView
,因此,我们需要将
swipedView
转换自动调整大小的GMaskintoConstraints
设置为
false
设置
swipedView
转换自动调整大小的GMaskintoConstraints
false
,而不是
视图

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var swipedView: UIView!
    @IBOutlet weak var foregroundView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // that was a proposed fix to my problem I found in the internet. Didn't work
        swipedView.translatesAutoresizingMaskIntoConstraints = false

    }

    override func viewDidAppear(animated: Bool) {

        let pinTop = NSLayoutConstraint(item: swipedView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0)
        let pinLeft = NSLayoutConstraint(item: swipedView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0)
        let pinRight = NSLayoutConstraint(item: swipedView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1.0, constant: 0)
        let pinBottom = NSLayoutConstraint(item: swipedView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0)

        NSLayoutConstraint.activateConstraints([pinTop, pinLeft, pinRight, pinBottom])

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

此解决方案最终起作用的原因可以在中找到

请注意,自动调整大小遮罩约束完全指定了视图的大小和位置;因此,在不引入冲突的情况下,无法添加其他约束来修改此大小或位置

translatesAutoResizengMaskintoConstraints
设置为
true
时,我们的意思是希望将相关视图的大小和位置转换为实际约束

通过将
translatesAutoResizengMaskintoConstraints
设置为
false
,我们可以说相关视图的大小和位置不应该因为任何原因而变成约束(在本例中,我们创建了自己的约束)


由于我们不希望应用约束来修改
视图的大小和位置,而实际上是
swipedView
,因此,我们需要将
swipedView
TranslatesAutoResizengMaskinToConstraints
设置为
false
,我在使用interface builder时也遇到过同样的问题,解决方案是识别导致警告的约束(在这种情况下,可能通过一次注释一个)然后将该约束的优先级设置为999,而不是1000(我认为这是默认值)。通过编程,您可以通过以下方式执行此操作:

constraint.priority = 999
(将“约束”替换为受影响的约束。)
这已经为我工作了好几次,并没有影响布局,但清除了警告。我认为999相当于说“尽可能靠近,但不要担心”,这样,如果约束条件不能满足一点点,你就不会收到警告。希望有帮助

我在使用interface builder时也遇到了同样的问题,解决方案是识别导致警告的约束(在本例中,可能通过一次注释一个),然后将该约束的优先级设置为999而不是1000(我认为这是默认设置)。通过编程,您可以通过以下方式执行此操作:

constraint.priority = 999
(将“约束”替换为受影响的约束。)
这已经为我工作了好几次,并没有影响布局,但清除了警告。我认为999相当于说“尽可能靠近,但不要担心”,这样,如果约束条件不能满足一点点,你就不会收到警告。希望有帮助

使用本文中提到的技术:

它对我不起作用的原因是我犯的错误

view.translatesAutoresizingMaskIntoConstraints=false

一定是这样

swipedView.translatesAutoResizengMaskintoConstraints=false

现在它起作用了。我不知道为什么(也许有些人可以解释?)


感谢@ZGski和@dan使用本文中提到的技巧:

它对我不起作用的原因是我犯的错误

view.translatesAutoresizingMaskIntoConstraints=false

一定是这样

swipedView.translatesAutoResizengMaskintoConstraints=false

现在它起作用了。我不知道为什么(也许有些人可以解释?)


谢谢@ZGski,@dan

试过了。虽然我没有找到这样一个函数,但只有一个布尔值设置为false。您尝试了被接受的答案链接到的
NSIBPrototypingLayoutConstraint
?我也尝试过,是的。现在它起作用了(我必须改变一些东西,见ZGski的答案)。我不知道为什么。我要发布一个答案。虽然我没有找到这样一个函数,但只有一个布尔值设置为false。您尝试了被接受的答案链接到的
NSIBPrototypingLayoutConstraint
?我也尝试过,是的。现在它起作用了(我必须改变一些东西,见ZGski的答案)。我不知道为什么。我将发布一个回答谢谢,但问题是每个约束都会导致这个错误(我试着逐一注释)。谢谢,但问题是每个约束都会导致这个错误(我试过)