无法同时满足约束 - 制图

时间:2015-09-23 04:11:43

标签: swift autolayout

我正在使用AutoLayout和我在Swift 2.0中编写的应用程序上的代码,并使用Cartograph来帮助约束。 但有些东西会阻止我的应用,否则,我会用它来以编程方式添加视图。

func addButtonOne() -> Void {
    buttonOne = UIButton()
    buttonOne.backgroundColor = UIColor.greenColor()
    self.view.addSubview(buttonOne)

    constrain(buttonOne, view) { buttonOne, view in
        buttonOne.left == view.right - 12
        buttonOne.right == view.left + 12
        buttonOne.bottom == view.bottom - 12
    }

}

但是在编译之后,它返回:

2015-09-23 00:54:24.489 Fun With Swift[24809:2358352] 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. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, 
refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(
"<NSLayoutConstraint:0x7afe2160 H:[UIView:0x7afdfe90]-(-12)-[UIButton:0x7afe13b0](LTR)>",
"<NSLayoutConstraint:0x7afe2730 UIButton:0x7afe13b0.right == UIView:0x7afdfe90.left + 12>",
"<NSLayoutConstraint:0x7b0866e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7afdfe90(320)]>"
)

任何人都知道我能做什么?这是一个不同的问题,因为我使用的是库。

1 个答案:

答案 0 :(得分:0)

添加约束代码应如下所示

 constrain(buttonOne, view) { buttonOne, view in
            buttonOne.left == view.left + 12
            buttonOne.right == view.right - 12
            buttonOne.bottom == view.bottom - 12
        }

如果你需要一个底部的按钮,从左下角开始有12磅的边距

相关问题