Sabur Aziz
Sabur Aziz

Reputation: 236

Autolayout Dumbness (Mine)?

I'm using auto layout in one of my projects and am having trouble with what I thought should be a trivial task.

As an example, I have two text boxes below each other. I have created a vertical spacing constraint between the two, of 5 points.

I would expect that if i move the top text box (using setFrame), the constraint would mean that the bottom one would automatically move with it to enforce the constraint?

Is my thinking correct? or am I using auto layout in a way that it was not meant to. ie. I have misunderstood its purpose. Or should I be moving the top text box using a different manner other than setFrame?

Any input would be great. Thanks in advance :-)

PS. I have played around with using: setTranslatesAutoresizingMaskIntoConstraints which seems to work. However, this doesn't seem like the proper way to do this. My initial thoughts are that auto layout is supposed to handle size/position relationships with little code, in this case, i would expect it would be handled with zero code.

Upvotes: 0

Views: 59

Answers (1)

Mike Gottlieb
Mike Gottlieb

Reputation: 966

You can't use setFrame to do that. Auto-layout decides the frame. If you change it after the fact the constraints aren't automatically updated.

You could get a reference to the constraint that decides the Y position (either when you create it programmatically or using an IBOutlet) of the top box and change its value. That would trigger an update to recompute the position of everything and run layout again.

You would do that by changing the value of the constant property on the NSLayoutConstraint object.

Reference Docs Here

Upvotes: 1

Related Questions