Manually specify restrictions in Xcode 4 interfacebuilder / storyboard

This seems like a really basic question, but I cannot find the answer to it.

When I add elements to the scene in my storyboard or drag and drop them or resize them, Xcode automatically adds and removes constraints to describe their size and positioning.

Now that Xcode has created a constraint of a certain type β€” say, a height constraint β€” I know that I can change its attributes β€” Relation , Constant and Priority in case of height constraint β€” through the attribute inspector. I can't figure out how to manually add or remove restrictions of a certain type, and not rely on Xcode magic to do this 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 for sure what restrictions I want to use to describe the vertical positioning and size of these two views:

  • The top of the toolbar has a vertical space of 0 at the top of the screen.
  • The toolbar has a fixed height
  • The bottom of the table has a vertical space of 0 at the bottom of the screen.
  • The top of the top of the table has a vertical space of 0 from the bottom of the toolbar

Thus, the height of the table will be adjusted accordingly to the screen size.

However, Xcode in its wisdom decided that this was not what I wanted, and instead imposed the following restrictions on me (which, it seems, do not want to change no matter how randomly I drag things and pray):

  • The top of the toolbar has a vertical space of 0 at the top of the screen.
  • The bottom of the toolbar has a vertical space 526 at the bottom of the screen.
  • The table view has a vertical space of 0 at the bottom of the screen.
  • The table view has a fixed height of 526

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

screenshot of EVERYTHING GOING WRONG

It’s not possible to just manually set your own limits, when I know exactly what I need, it is frustrating. How can I explicitly remove the flash limits that Xcode automatically created and manually add my own instead?

+6
source share
1 answer

The problem with 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 of editing. Therefore, when you can edit several restrictions, Xcode may decide to automatically add some to prevent a forbidden state.

It can be painful for work. What I have found is to add dummy constraints for all four edges while I set other constraints. This, I hope, keeps IB in check and prevents him from adding completely dumb stuff.

You can add restrictions from the top menu. Click an object, and then an editor on the menu. You can add restrictions using the Align and Pin submenus (which can later be edited).

Note: not all restrictions may be added to IB. Aspect ratio, for example, can only be done in code.

PS. Xcode will allow you to remove unnecessary restrictions, i.e. This will not lead to an ambiguous condition. Therefore, in order to remove the restrictions of "b **** t", you must first add enough others to create an allowed state.

+1
source

Source: https://habr.com/ru/post/948705/


All Articles