Reputation: 4187
I would like to put side by side two buttons.
One ( testButton
) is currently at the right position - Vertically and Horizontally aligned - with Interface Builder Auto Layout.
Let's look :
I would like to put the other one ( okButton
) programmatically side by side :
Preview of what I want :
I added a CenterY constraint and a Leading/Trailing one but it does not work, the UIButton width of the test button grow..
Let's take a look at the Debug View Hierarchy
:
And my code :
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)
}
Warning console :
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)
Any idea ? Thanks !
Upvotes: 0
Views: 2103
Reputation: 104082
If you add a view, and don't add any constraints yourself, the system adds them for you (to the top and left side), so I think that's the problem.
Those system added constraints are conflicting with the ones you added in code. Since you ultimate goal is to add views dynamically, you should add the "ok" button in code rather than IB. Be sure to set translatesAutoresizingMaskIntoConstraints
to false
for any code added views.
If you really need to add that button in IB, then give it constraints, but have them be removed, by checking the "remove at build time" check box. Then it will be safe to add new ones in code.
Upvotes: 1