samedi 9 mai 2015

How do I programmatically put constraint to buttons (horizontal spacing)

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 :

Interface builder - auto layout - currently

I would like to put the other one ( okButton ) programmatically side by side :

Preview of what I want :

Interface builder - auto layout - result

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 :

Interface builder - auto layout - 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 !

Aucun commentaire:

Enregistrer un commentaire