Sometimes the autolayout constraints snap to a wrong anchor point – like when I want a control to be anchored to a control above it and the autolayout constraint gets anchored to the bottom of the window. Is there a way to move the constraint? An ASCII art should make this more clear. This is what I have:
+------------------------+
| |
| [x] Some checkbox |
| |
| [x] Another checkbox |
| | |
| | ← autolayout |
| | constraint |
| | |
+-----+------------------+
And this is what I want instead:
+------------------------+
| |
| [x] Some checkbox |
| | ← A.C. |
| [x] Another checkbox |
| |
| |
| |
| |
+------------------------+
I can anchor the constraint to the upper checkbox by moving the lower checkbox upwards, but that’s not what I want, I want the vertical space to be larger. (The checkboxes are just for an illustration, my real use case has different views.)
Now select the useless constraints and delete it:
Try to move the lower checkbox so that it get's a strut between itself and the checkbox above. Then click on the strut and click the attributes box in the right panel. There, you should be able to uncheck the "standard" box and set it to the spacing you want.
No, you can't move a constraint or change it to be relative to a different view. Like David Chu says, you can move the views until it automatic gets the kind of constraint you want and then change the constraint's value to shift the view back where you want. However, you can also manually create the constraint you want and then delete the automatic one you don't (sometimes with some difficulty). Here's how for your case:
Select the 2 checkboxes, then use the Editor > Pin > Vertical Spacing menu to add the constraint you want (or use the middle of the 3 buttons at the bottom right of the IB layout view to get the same menu). At that point the unwanted, automatic constraint should become redundant and switch from purple to blue. You can then select it and delete it. Each kind of constraints can be created using the items in those Pin and Align menus.
I've found that getting the constraints you want is always possible, although its a battle against the automatic constraints that Xcode adds, and sometimes a puzzle to find why they aren't shifting from purple to blue. For example, directly moving a view after adding some custom constraint and you'll find that Xcode has probably deleted most of the constraints you added and re-added automatic ones, even for other seemingly random views within the window, not just the one you moved! Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With