如何以编程方式将约束放入按钮(水平间距)

时间:2015-05-09 21:09:36

标签: ios xcode swift autolayout

我想把两个按钮放在一起。

一个(testButton)当前位于正确位置 - 垂直和水平对齐 - 使用Interface Builder自动布局。

让我们看看:

Interface builder - auto layout - currently

我想将另一个(okButton)以编程方式并排放置:

预览我想要的内容:

Interface builder - auto layout - result

enter image description here

我添加了一个CenterY约束和一个前导/尾随但它不起作用,测试按钮的UIButton宽度增长..

我们来看看Debug View Hierarchy

Interface builder - auto layout - Debug View Hierarchy

我的代码:

override func viewDidLoad() {
        super.viewDidLoad()

        let constraintAlignY = NSLayoutConstraint(
            item: okButton,
            attribute: .CenterY,
            relatedBy: .Equal,
            toItem: testButton,
            attribute: .CenterY,
            multiplier: 1,
            constant: 0)

        let constraintXSpace = NSLayoutConstraint(
            item: okButton,
            attribute: .Leading,
            relatedBy: .Equal,
            toItem: testButton,
            attribute: .Trailing,
            multiplier: 1,
            constant: 8)

        view.addConstraint(constraintAlignY)
        view.addConstraint(constraintXSpace)
    }

警告控制台:

2015-05-09 23:03:10.439 MainThreading[10227:663061] 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:0x7b8c8ee0 UIButton:0x7b8cde90'test'.centerY == UIView:0x7b8ced40.centerY>",
    "<NSIBPrototypingLayoutConstraint:0x7b8c40f0 'IB auto generated at build time for view with fixed frame' V:|-(191)-[UIButton:0x7b8ce8d0'ok']   (Names: '|':UIView:0x7b8ced40 )>",
    "<NSIBPrototypingLayoutConstraint:0x7b8c77f0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7b8ce8d0'ok'(30)]>",
    "<NSLayoutConstraint:0x7b8c4f50 UIButton:0x7b8ce8d0'ok'.centerY == UIButton:0x7b8cde90'test'.centerY>",
    "<NSLayoutConstraint:0x7b8da380 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b8ced40(480)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7b8c4f50 UIButton:0x7b8ce8d0'ok'.centerY == UIButton:0x7b8cde90'test'.centerY>

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.
(lldb) 

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

如果你添加一个视图,而不是自己添加任何约束,系统会为你添加它们(在顶部和左侧),所以我认为这就是问题所在。

那些系统添加的约束与您在代码中添加的约束相冲突。由于您的最终目标是动态添加视图,因此您应该添加&#34; ok&#34;代码中的按钮而不是IB。对于添加了任何代码的视图,请务必将translatesAutoresizingMaskIntoConstraints设置为false

如果你真的需要在IB中添加该按钮,那么给它约束,但是通过检查&#34; 在构建时删除&#34;来删除它们。复选框。然后在代码中添加新的将是安全的。