Jay Foreman
Jay Foreman

Reputation: 600

Why are NSLayoutConstraints ignored?

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.

Upvotes: 2

Views: 295

Answers (1)

Jonathan
Jonathan

Reputation: 2383

On your UIButton (btn) set translatesAutoresizingMaskIntoConstraints = false

E.g.

btn.translatesAutoresizingMaskIntoConstraints = false

Recommended reading: Adopting Auto Layout

Upvotes: 5

Related Questions