Marcal
Marcal

Reputation: 1371

Autolayout Constraint Error with a single constraint

I have a scence with a bunch of labels and buttons. When you tap on a button a view slides up from the bottom with controls in it, a kind of keyboard so to say. It "looks" like this:

 -----------------------------
|                             |
|  [Button 1] [  Slider 1  ]  |
|                             |
|  [Button 2] [  Slider 2  ]  |
|                             |
 -----------------------------

This "keyboard" is created at the very beginning when the view loads and the animation is done switching its heigth from 0 to its instrinsic content size. This escene only supports landscape mode and it took me quite a while to keep the "keyboard" on the view when the device rotates 180 degrees.

The problem I see pops up with either of the two following situations:

  1. The device rotates 180 degrees.
  2. The "keyboard" is called.

This problem as follows:

Unable to simultaneously satisfy constraints...
.
.
.
.
(
    "<NSLayoutConstraint:0x718c6c0 UIButton:0x717e0d0.centerY == UISlider:0x717d9d0.centerY>",
    "<NSLayoutConstraint:0x7190a00 UIButton:0x717e0d0.centerY == UISlider:0x717d9d0.centerY>"
)

The error log gives me this error twice, once for each set of button-slider.

What I think is weird is that the conflicting constraints are exactly the same. I thought I did some copy-paste mistake and added the same constraint twice, but it's not the case.

I'm guessing it has something to do with updateViewConstraints being called upon rotation and also whe I perform the animation, but I cannot see why only these constraints are affected since there are some more in this "keyboard" view.

All and all, this Autolayout is beign quite more difficult than Apples whants to claim. In my opinion, of course.

Any ideas?

EDIT: the constrains are set all in code using mainly the visual language format. The constrains of the controls inside the "keyboard" are added into the "keyboard" view which is the normal thing to do, I believe.

Just to try it out, I changed the problematic constraints and, instead to adding them to the "keyboard" subview, I added them to the self.view ("keyboard" superview). All the sudden, no more errors are shown.

Despite of that, I'd really like some discussion on the matter because I still don't know what's wrong and I just had a lucky shot. I'd really like to undestand it.

Upvotes: 3

Views: 378

Answers (2)

bachman
bachman

Reputation: 690

I don't know if this may help you but I just used this tutorial for setting up constraints to buttons and labels in my app

Upvotes: 1

Max MacLeod
Max MacLeod

Reputation: 26652

The fact that the conflicting constraints are exactly the same is in fact the error. With Auto Layout, you can't have a constraint twice. That will generate the error you see above.

Definitely, you have added the constraint twice. You can see this from the memory addresses. You have two different NSLayoutConstraint instances, 0x718c6c0 and 0x7190a00. However, the instances each refers to are both the same. That being that the vertical center centerY of your UIButton instance 0x717e0d0 should be in the middle of the UISlider 0x717d9d0.

Possibly your updateConstraints method has been called and you haven't checked to see if a constraint already exists before adding it again.

Upvotes: 2

Related Questions