Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually specify constraints in Xcode 4 interfacebuilder/storyboard

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.

screenshot showing the same stuff described in the paragraph above

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:

  • The top of the toolbar has Vertical Space 0 from the top of the screen
  • The toolbar has a fixed height
  • The bottom of the table view has Vertical Space 0 from the bottom of the screen
  • The top of the table view has Vertical Space 0 from the bottom of the toolbar

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):

  • The top of the toolbar has Vertical Space 0 from the top of the screen
  • The bottom of the toolbar has Vertical Space 526 from the bottom of the screen
  • The table view has Vertical Space 0 from the bottom of the screen
  • The table view has a fixed height of 526

As a result, everything goes wrong when I try viewing my ViewController on a smaller screen, or in a container:

screenshot of EVERYTHING GOING WRONG

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?

like image 988
Mark Amery Avatar asked Jul 04 '13 11:07

Mark Amery


1 Answers

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.

like image 134
user1459524 Avatar answered Oct 21 '22 00:10

user1459524