2
votes

I am experiencing a misunderstanding of mechanics of iOS Layout Constraints. See the code I placed inside viewDidLoad listed below.

    var btn = UIButton()
    btn.setTitle("i am a button", forState: UIControlState.Normal)
    btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    btn.backgroundColor = UIColor.lightGrayColor()
    btn.sizeToFit()

    view.addSubview(btn)

    view.addConstraint(
        NSLayoutConstraint(item: view,
            attribute: NSLayoutAttribute.CenterX,
            relatedBy: NSLayoutRelation.Equal,
            toItem: btn,
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1.0,
            constant: 0.0))

    view.addConstraint(
        NSLayoutConstraint(item: view,
            attribute: NSLayoutAttribute.CenterY,
            relatedBy: NSLayoutRelation.Equal,
            toItem: btn,
            attribute: NSLayoutAttribute.CenterY,
            multiplier: 1.0,
            constant: 0.0))

It seems to me my intention is clear. I want to see a button at the center of a device's screen. But I can see only the following picture though.

iPhone 6 Simulator

And I have an output in project’s console so scary I cannot understand a thing from it.

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) ( "", "", "", " (Names: '|':UIWindow:0x7fd318551080 )>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2015-04-28 23:46:04.516 ConsTest[5966:248434] 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) ( "", "", "", " (Names: '|':UIWindow:0x7fd318551080 )>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

Looks like the constraints are treated contradictory and thus ignored at all. I cannot really point out why I can’t just create a button and place it at the center programmatically. Any relevant instruction is much appreciated.

1
I'm not sure if this is the problem, have you tried inverting the view parameter's order in the constraint parameters? The first one should be the left view in the constraint composition. - Tiago Maia
@TiagoMaria as it is said in NSLayoutConstraint Class Reference, constraints are equations (and not assignment operators) means that you can switch the order of the items in the equation as needed. So, no, inverting of the parameters’ order gives nothing. - Jay Foreman

1 Answers

5
votes

On your UIButton (btn) set translatesAutoresizingMaskIntoConstraints = false

E.g.

btn.translatesAutoresizingMaskIntoConstraints = false

Recommended reading: Adopting Auto Layout