This seems like a really basic question, but I can't find the answer to it.
When I add elements to a scene in my storyboard, or drag them around or resize them, Xcode automatically adds and removes constraints to describe their sizing and positioning.
Now, once Xcode has created a constraint of a particular type - say, a height constraint - I know that I can modify its attributes - Relation
, Constant
and Priority
in the case of the height constraint - via the attributes inspector. What I can't figure out is how to manually add or remove constraints of a given type rather than relying on Xcode's magic to do so for me.
For example, in my current scenario, I have a ViewController that contains a toolbar and a table view. Now, I know exactly what constraints I want to use to describe the vertical positioning and sizing of those two views:
This way the table view's height will adjust appropriately to the screen size.
However, Xcode, in its wisdom, has decided that this isn't what I want, and has instead inflicted the following constraints on me (which it doesn't seem to want to change no matter how much I randomly drag stuff around and pray):
As a result, everything goes wrong when I try viewing my ViewController on a smaller screen, or in a container:
Not being able to just manually set my own constraints when I know exactly what I need is frustrating. How can I explicitly delete the bullshit constraints that Xcode has automatically created and manually add my own instead?
The problem when working with constraints in IB/storyboard is that Xcode will never allow you to have ambiguous constraints. Ever. Including when you are in the process if editing them. So whenever you may want to edit multiple constraints, Xcode may decide to automatically add some to prevent a disallowed state.
This can be painful to work with. What I have found kind of works is adding bogus constraints on all four edges while I am setting other constraints. This - hopefully - keeps IB in check and prevents it from adding completely dumb stuff.
You can add constraints from the top menu. Click the object and then Editor from the to menu. You can add constraints with the Align and Pin submenus (which then can be edited later on).
Note: not all constraints can be added in IB. Aspect ratio for example can only be done in code.
PS. Xcode will only allow you to delete constraints that are redundant, i.e that are not going to leave an ambiguous state. So in order to delete the "b****t" constraints, you have to first add enough others to create an allowed state.
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